summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/functions.rs
blob: 0aef60959593b1c0ed02209a3158d79547711aa3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#![warn(clippy::all)]
#![allow(dead_code, unused_unsafe)]
#![allow(clippy::missing_safety_doc, clippy::uninlined_format_args)]

// TOO_MANY_ARGUMENTS
fn good(_one: u32, _two: u32, _three: &str, _four: bool, _five: f32, _six: f32, _seven: bool) {}

fn bad(_one: u32, _two: u32, _three: &str, _four: bool, _five: f32, _six: f32, _seven: bool, _eight: ()) {}
//~^ ERROR: this function has too many arguments (8/7)
//~| NOTE: `-D clippy::too-many-arguments` implied by `-D warnings`

#[rustfmt::skip]
fn bad_multiline(
//~^ ERROR: this function has too many arguments (8/7)
    one: u32,
    two: u32,
    three: &str,
    four: bool,
    five: f32,
    six: f32,
    seven: bool,
    eight: ()
) {
    let _one = one;
    let _two = two;
    let _three = three;
    let _four = four;
    let _five = five;
    let _six = six;
    let _seven = seven;
}

// don't lint extern fns
extern "C" fn extern_fn(
    _one: u32,
    _two: u32,
    _three: *const u8,
    _four: bool,
    _five: f32,
    _six: f32,
    _seven: bool,
    _eight: *const std::ffi::c_void,
) {
}

pub trait Foo {
    fn good(_one: u32, _two: u32, _three: &str, _four: bool, _five: f32, _six: f32, _seven: bool);
    fn bad(_one: u32, _two: u32, _three: &str, _four: bool, _five: f32, _six: f32, _seven: bool, _eight: ());
    //~^ ERROR: this function has too many arguments (8/7)

    fn ptr(p: *const u8);
}

pub struct Bar;

impl Bar {
    fn good_method(_one: u32, _two: u32, _three: &str, _four: bool, _five: f32, _six: f32, _seven: bool) {}
    fn bad_method(_one: u32, _two: u32, _three: &str, _four: bool, _five: f32, _six: f32, _seven: bool, _eight: ()) {}
    //~^ ERROR: this function has too many arguments (8/7)
}

// ok, we don’t want to warn implementations
impl Foo for Bar {
    fn good(_one: u32, _two: u32, _three: &str, _four: bool, _five: f32, _six: f32, _seven: bool) {}
    fn bad(_one: u32, _two: u32, _three: &str, _four: bool, _five: f32, _six: f32, _seven: bool, _eight: ()) {}

    fn ptr(p: *const u8) {
        println!("{}", unsafe { *p });
        //~^ ERROR: this public function might dereference a raw pointer but is not marked
        //~| NOTE: `-D clippy::not-unsafe-ptr-arg-deref` implied by `-D warnings`
        println!("{:?}", unsafe { p.as_ref() });
        //~^ ERROR: this public function might dereference a raw pointer but is not marked
        unsafe { std::ptr::read(p) };
        //~^ ERROR: this public function might dereference a raw pointer but is not marked
    }
}

// NOT_UNSAFE_PTR_ARG_DEREF

fn private(p: *const u8) {
    println!("{}", unsafe { *p });
}

pub fn public(p: *const u8) {
    println!("{}", unsafe { *p });
    //~^ ERROR: this public function might dereference a raw pointer but is not marked `un
    println!("{:?}", unsafe { p.as_ref() });
    //~^ ERROR: this public function might dereference a raw pointer but is not marked `un
    unsafe { std::ptr::read(p) };
    //~^ ERROR: this public function might dereference a raw pointer but is not marked `un
}

type Alias = *const u8;

pub fn type_alias(p: Alias) {
    println!("{}", unsafe { *p });
    //~^ ERROR: this public function might dereference a raw pointer but is not marked `un
    println!("{:?}", unsafe { p.as_ref() });
    //~^ ERROR: this public function might dereference a raw pointer but is not marked `un
    unsafe { std::ptr::read(p) };
    //~^ ERROR: this public function might dereference a raw pointer but is not marked `un
}

impl Bar {
    fn private(self, p: *const u8) {
        println!("{}", unsafe { *p });
    }

    pub fn public(self, p: *const u8) {
        println!("{}", unsafe { *p });
        //~^ ERROR: this public function might dereference a raw pointer but is not marked
        println!("{:?}", unsafe { p.as_ref() });
        //~^ ERROR: this public function might dereference a raw pointer but is not marked
        unsafe { std::ptr::read(p) };
        //~^ ERROR: this public function might dereference a raw pointer but is not marked
    }

    pub fn public_ok(self, p: *const u8) {
        if !p.is_null() {
            println!("{:p}", p);
        }
    }

    pub unsafe fn public_unsafe(self, p: *const u8) {
        println!("{}", unsafe { *p });
        println!("{:?}", unsafe { p.as_ref() });
    }
}

fn main() {}