Skip to content

Commit 0376ceb

Browse files
committed
Take into account lint attributes in single_call_fn
+ other misc changes.
1 parent d764a0e commit 0376ceb

16 files changed

+97
-134
lines changed

clippy_lints/src/needless_pass_by_ref_mut.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_target::spec::abi::Abi;
1919

2020
declare_clippy_lint! {
2121
/// ### What it does
22-
/// Check if a `&mut` function argument is actually used mutably.
22+
/// Checks if a `&mut` function argument is actually used mutably.
2323
///
2424
/// Be careful if the function is publicly reexported as it would break compatibility with
2525
/// users of this function.
@@ -83,7 +83,6 @@ fn should_skip<'tcx>(
8383
}
8484
}
8585

86-
// All spans generated from a proc-macro invocation are the same...
8786
is_from_proc_macro(cx, &input)
8887
}
8988

@@ -171,11 +170,11 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByRefMut {
171170
cx,
172171
NEEDLESS_PASS_BY_REF_MUT,
173172
sp,
174-
"this argument is a mutable reference, but not used mutably",
173+
"this argument is a mutable reference, but never used mutably",
175174
|diag| {
176175
diag.span_suggestion(
177176
sp,
178-
"consider changing to".to_string(),
177+
"consider using an immutable reference instead",
179178
format!(
180179
"&{}",
181180
snippet(cx, cx.tcx.hir().span(inner_ty.ty.hir_id), "_"),

clippy_lints/src/single_call_fn.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::span_lint_and_help;
1+
use clippy_utils::diagnostics::span_lint_hir_and_then;
22
use clippy_utils::{is_from_proc_macro, is_in_test_function};
33
use rustc_data_structures::fx::FxHashMap;
44
use rustc_hir::def_id::LocalDefId;
@@ -88,16 +88,18 @@ impl<'tcx> LateLintPass<'tcx> for SingleCallFn {
8888
};
8989
cx.tcx.hir().visit_all_item_likes_in_crate(&mut v);
9090

91-
for usage in self.def_id_to_usage.values() {
91+
for (def_id, usage) in &self.def_id_to_usage {
9292
let single_call_fn_span = usage.0;
9393
if let [caller_span] = *usage.1 {
94-
span_lint_and_help(
94+
span_lint_hir_and_then(
9595
cx,
9696
SINGLE_CALL_FN,
97+
cx.tcx.hir().local_def_id_to_hir_id(*def_id),
9798
single_call_fn_span,
9899
"this function is only used once",
99-
Some(caller_span),
100-
"used here",
100+
|diag| {
101+
diag.span_help(caller_span, "used here");
102+
},
101103
);
102104
}
103105
}

tests/ui/infinite_loop.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(clippy::needless_pass_by_ref_mut)]
2+
13
fn fn_val(i: i32) -> i32 {
24
unimplemented!()
35
}

tests/ui/infinite_loop.stderr

+12-20
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
error: this argument is a mutable reference, but not used mutably
2-
--> $DIR/infinite_loop.rs:7:17
3-
|
4-
LL | fn fn_mutref(i: &mut i32) {
5-
| ^^^^^^^^ help: consider changing to: `&i32`
6-
|
7-
= note: `-D clippy::needless-pass-by-ref-mut` implied by `-D warnings`
8-
91
error: variables in the condition are not mutated in the loop body
10-
--> $DIR/infinite_loop.rs:20:11
2+
--> $DIR/infinite_loop.rs:22:11
113
|
124
LL | while y < 10 {
135
| ^^^^^^
@@ -16,71 +8,71 @@ LL | while y < 10 {
168
= note: `#[deny(clippy::while_immutable_condition)]` on by default
179

1810
error: variables in the condition are not mutated in the loop body
19-
--> $DIR/infinite_loop.rs:25:11
11+
--> $DIR/infinite_loop.rs:27:11
2012
|
2113
LL | while y < 10 && x < 3 {
2214
| ^^^^^^^^^^^^^^^
2315
|
2416
= note: this may lead to an infinite or to a never running loop
2517

2618
error: variables in the condition are not mutated in the loop body
27-
--> $DIR/infinite_loop.rs:32:11
19+
--> $DIR/infinite_loop.rs:34:11
2820
|
2921
LL | while !cond {
3022
| ^^^^^
3123
|
3224
= note: this may lead to an infinite or to a never running loop
3325

3426
error: variables in the condition are not mutated in the loop body
35-
--> $DIR/infinite_loop.rs:76:11
27+
--> $DIR/infinite_loop.rs:78:11
3628
|
3729
LL | while i < 3 {
3830
| ^^^^^
3931
|
4032
= note: this may lead to an infinite or to a never running loop
4133

4234
error: variables in the condition are not mutated in the loop body
43-
--> $DIR/infinite_loop.rs:81:11
35+
--> $DIR/infinite_loop.rs:83:11
4436
|
4537
LL | while i < 3 && j > 0 {
4638
| ^^^^^^^^^^^^^^
4739
|
4840
= note: this may lead to an infinite or to a never running loop
4941

5042
error: variables in the condition are not mutated in the loop body
51-
--> $DIR/infinite_loop.rs:85:11
43+
--> $DIR/infinite_loop.rs:87:11
5244
|
5345
LL | while i < 3 {
5446
| ^^^^^
5547
|
5648
= note: this may lead to an infinite or to a never running loop
5749

5850
error: variables in the condition are not mutated in the loop body
59-
--> $DIR/infinite_loop.rs:100:11
51+
--> $DIR/infinite_loop.rs:102:11
6052
|
6153
LL | while i < 3 {
6254
| ^^^^^
6355
|
6456
= note: this may lead to an infinite or to a never running loop
6557

6658
error: variables in the condition are not mutated in the loop body
67-
--> $DIR/infinite_loop.rs:105:11
59+
--> $DIR/infinite_loop.rs:107:11
6860
|
6961
LL | while i < 3 {
7062
| ^^^^^
7163
|
7264
= note: this may lead to an infinite or to a never running loop
7365

7466
error: variables in the condition are not mutated in the loop body
75-
--> $DIR/infinite_loop.rs:171:15
67+
--> $DIR/infinite_loop.rs:173:15
7668
|
7769
LL | while self.count < n {
7870
| ^^^^^^^^^^^^^^
7971
|
8072
= note: this may lead to an infinite or to a never running loop
8173

8274
error: variables in the condition are not mutated in the loop body
83-
--> $DIR/infinite_loop.rs:179:11
75+
--> $DIR/infinite_loop.rs:181:11
8476
|
8577
LL | while y < 10 {
8678
| ^^^^^^
@@ -90,7 +82,7 @@ LL | while y < 10 {
9082
= help: rewrite it as `if cond { loop { } }`
9183

9284
error: variables in the condition are not mutated in the loop body
93-
--> $DIR/infinite_loop.rs:186:11
85+
--> $DIR/infinite_loop.rs:188:11
9486
|
9587
LL | while y < 10 {
9688
| ^^^^^^
@@ -99,5 +91,5 @@ LL | while y < 10 {
9991
= note: this loop contains `return`s or `break`s
10092
= help: rewrite it as `if cond { loop { } }`
10193

102-
error: aborting due to 12 previous errors
94+
error: aborting due to 11 previous errors
10395

tests/ui/let_underscore_future.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(clippy::needless_pass_by_ref_mut)]
2+
13
use std::future::Future;
24

35
async fn some_async_fn() {}

tests/ui/let_underscore_future.stderr

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
error: this argument is a mutable reference, but not used mutably
2-
--> $DIR/let_underscore_future.rs:11:35
3-
|
4-
LL | fn do_something_to_future(future: &mut impl Future<Output = ()>) {}
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing to: `&impl Future<Output = ()>`
6-
|
7-
= note: `-D clippy::needless-pass-by-ref-mut` implied by `-D warnings`
8-
91
error: non-binding `let` on a future
10-
--> $DIR/let_underscore_future.rs:14:5
2+
--> $DIR/let_underscore_future.rs:16:5
113
|
124
LL | let _ = some_async_fn();
135
| ^^^^^^^^^^^^^^^^^^^^^^^^
@@ -16,20 +8,20 @@ LL | let _ = some_async_fn();
168
= note: `-D clippy::let-underscore-future` implied by `-D warnings`
179

1810
error: non-binding `let` on a future
19-
--> $DIR/let_underscore_future.rs:15:5
11+
--> $DIR/let_underscore_future.rs:17:5
2012
|
2113
LL | let _ = custom();
2214
| ^^^^^^^^^^^^^^^^^
2315
|
2416
= help: consider awaiting the future or dropping explicitly with `std::mem::drop`
2517

2618
error: non-binding `let` on a future
27-
--> $DIR/let_underscore_future.rs:19:5
19+
--> $DIR/let_underscore_future.rs:21:5
2820
|
2921
LL | let _ = future;
3022
| ^^^^^^^^^^^^^^^
3123
|
3224
= help: consider awaiting the future or dropping explicitly with `std::mem::drop`
3325

34-
error: aborting due to 4 previous errors
26+
error: aborting due to 3 previous errors
3527

tests/ui/mut_key.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(clippy::needless_pass_by_ref_mut)]
2+
13
use std::cell::Cell;
24
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
35
use std::hash::{Hash, Hasher};

tests/ui/mut_key.stderr

+18-26
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,106 @@
11
error: mutable key type
2-
--> $DIR/mut_key.rs:31:32
2+
--> $DIR/mut_key.rs:33:32
33
|
44
LL | fn should_not_take_this_arg(m: &mut HashMap<Key, usize>, _n: usize) -> HashSet<Key> {
55
| ^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::mutable-key-type` implied by `-D warnings`
88

99
error: mutable key type
10-
--> $DIR/mut_key.rs:31:72
10+
--> $DIR/mut_key.rs:33:72
1111
|
1212
LL | fn should_not_take_this_arg(m: &mut HashMap<Key, usize>, _n: usize) -> HashSet<Key> {
1313
| ^^^^^^^^^^^^
1414

15-
error: this argument is a mutable reference, but not used mutably
16-
--> $DIR/mut_key.rs:31:32
17-
|
18-
LL | fn should_not_take_this_arg(m: &mut HashMap<Key, usize>, _n: usize) -> HashSet<Key> {
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing to: `&HashMap<Key, usize>`
20-
|
21-
= note: `-D clippy::needless-pass-by-ref-mut` implied by `-D warnings`
22-
2315
error: mutable key type
24-
--> $DIR/mut_key.rs:32:5
16+
--> $DIR/mut_key.rs:34:5
2517
|
2618
LL | let _other: HashMap<Key, bool> = HashMap::new();
2719
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2820

2921
error: mutable key type
30-
--> $DIR/mut_key.rs:59:22
22+
--> $DIR/mut_key.rs:61:22
3123
|
3224
LL | fn tuples_bad<U>(_m: &mut HashMap<(Key, U), bool>) {}
3325
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3426

3527
error: mutable key type
36-
--> $DIR/mut_key.rs:71:5
28+
--> $DIR/mut_key.rs:73:5
3729
|
3830
LL | let _map = HashMap::<Cell<usize>, usize>::new();
3931
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4032

4133
error: mutable key type
42-
--> $DIR/mut_key.rs:72:5
34+
--> $DIR/mut_key.rs:74:5
4335
|
4436
LL | let _map = HashMap::<&mut Cell<usize>, usize>::new();
4537
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4638

4739
error: mutable key type
48-
--> $DIR/mut_key.rs:73:5
40+
--> $DIR/mut_key.rs:75:5
4941
|
5042
LL | let _map = HashMap::<&mut usize, usize>::new();
5143
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5244

5345
error: mutable key type
54-
--> $DIR/mut_key.rs:75:5
46+
--> $DIR/mut_key.rs:77:5
5547
|
5648
LL | let _map = HashMap::<Vec<Cell<usize>>, usize>::new();
5749
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5850

5951
error: mutable key type
60-
--> $DIR/mut_key.rs:76:5
52+
--> $DIR/mut_key.rs:78:5
6153
|
6254
LL | let _map = HashMap::<BTreeMap<Cell<usize>, ()>, usize>::new();
6355
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6456

6557
error: mutable key type
66-
--> $DIR/mut_key.rs:77:5
58+
--> $DIR/mut_key.rs:79:5
6759
|
6860
LL | let _map = HashMap::<BTreeMap<(), Cell<usize>>, usize>::new();
6961
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7062

7163
error: mutable key type
72-
--> $DIR/mut_key.rs:78:5
64+
--> $DIR/mut_key.rs:80:5
7365
|
7466
LL | let _map = HashMap::<BTreeSet<Cell<usize>>, usize>::new();
7567
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7668

7769
error: mutable key type
78-
--> $DIR/mut_key.rs:79:5
70+
--> $DIR/mut_key.rs:81:5
7971
|
8072
LL | let _map = HashMap::<Option<Cell<usize>>, usize>::new();
8173
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8274

8375
error: mutable key type
84-
--> $DIR/mut_key.rs:80:5
76+
--> $DIR/mut_key.rs:82:5
8577
|
8678
LL | let _map = HashMap::<Option<Vec<Cell<usize>>>, usize>::new();
8779
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8880

8981
error: mutable key type
90-
--> $DIR/mut_key.rs:81:5
82+
--> $DIR/mut_key.rs:83:5
9183
|
9284
LL | let _map = HashMap::<Result<&mut usize, ()>, usize>::new();
9385
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9486

9587
error: mutable key type
96-
--> $DIR/mut_key.rs:83:5
88+
--> $DIR/mut_key.rs:85:5
9789
|
9890
LL | let _map = HashMap::<Box<Cell<usize>>, usize>::new();
9991
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10092

10193
error: mutable key type
102-
--> $DIR/mut_key.rs:84:5
94+
--> $DIR/mut_key.rs:86:5
10395
|
10496
LL | let _map = HashMap::<Rc<Cell<usize>>, usize>::new();
10597
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10698

10799
error: mutable key type
108-
--> $DIR/mut_key.rs:85:5
100+
--> $DIR/mut_key.rs:87:5
109101
|
110102
LL | let _map = HashMap::<Arc<Cell<usize>>, usize>::new();
111103
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
112104

113-
error: aborting due to 18 previous errors
105+
error: aborting due to 17 previous errors
114106

tests/ui/mut_reference.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![allow(unused_variables)]
1+
#![allow(clippy::needless_pass_by_ref_mut, unused_variables)]
22

33
fn takes_an_immutable_reference(a: &i32) {}
44
fn takes_a_mutable_reference(a: &mut i32) {}

tests/ui/mut_reference.stderr

+1-15
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,3 @@
1-
error: this argument is a mutable reference, but not used mutably
2-
--> $DIR/mut_reference.rs:4:33
3-
|
4-
LL | fn takes_a_mutable_reference(a: &mut i32) {}
5-
| ^^^^^^^^ help: consider changing to: `&i32`
6-
|
7-
= note: `-D clippy::needless-pass-by-ref-mut` implied by `-D warnings`
8-
9-
error: this argument is a mutable reference, but not used mutably
10-
--> $DIR/mut_reference.rs:11:44
11-
|
12-
LL | fn takes_a_mutable_reference(&self, a: &mut i32) {}
13-
| ^^^^^^^^ help: consider changing to: `&i32`
14-
151
error: the function `takes_an_immutable_reference` doesn't need a mutable reference
162
--> $DIR/mut_reference.rs:17:34
173
|
@@ -32,5 +18,5 @@ error: the method `takes_an_immutable_reference` doesn't need a mutable referenc
3218
LL | my_struct.takes_an_immutable_reference(&mut 42);
3319
| ^^^^^^^
3420

35-
error: aborting due to 5 previous errors
21+
error: aborting due to 3 previous errors
3622

0 commit comments

Comments
 (0)