Skip to content

Commit 8494a06

Browse files
committed
Take into account lint attributes in single_call_fn
+ other misc changes.
1 parent 588c1ab commit 8494a06

16 files changed

+122
-153
lines changed

clippy_lints/src/needless_pass_by_ref_mut.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_target::spec::abi::Abi;
2121

2222
declare_clippy_lint! {
2323
/// ### What it does
24-
/// Check if a `&mut` function argument is actually used mutably.
24+
/// Checks if a `&mut` function argument is actually used mutably.
2525
///
2626
/// Be careful if the function is publicly reexported as it would break compatibility with
2727
/// users of this function.
@@ -89,7 +89,6 @@ fn should_skip<'tcx>(
8989
}
9090
}
9191

92-
// All spans generated from a proc-macro invocation are the same...
9392
is_from_proc_macro(cx, &input)
9493
}
9594

@@ -203,11 +202,11 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByRefMut<'tcx> {
203202
NEEDLESS_PASS_BY_REF_MUT,
204203
cx.tcx.hir().local_def_id_to_hir_id(*fn_def_id),
205204
sp,
206-
"this argument is a mutable reference, but not used mutably",
205+
"this argument is a mutable reference, but never used mutably",
207206
|diag| {
208207
diag.span_suggestion(
209208
sp,
210-
"consider changing to".to_string(),
209+
"consider using an immutable reference instead",
211210
format!("&{}", snippet(cx, cx.tcx.hir().span(inner_ty.ty.hir_id), "_"),),
212211
Applicability::Unspecified,
213212
);

clippy_lints/src/single_call_fn.rs

Lines changed: 7 additions & 5 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: variables in the condition are not mutated in the loop body
2-
--> $DIR/infinite_loop.rs:20:11
2+
--> $DIR/infinite_loop.rs:22:11
33
|
44
LL | while y < 10 {
55
| ^^^^^^
@@ -8,71 +8,71 @@ LL | while y < 10 {
88
= note: `#[deny(clippy::while_immutable_condition)]` on by default
99

1010
error: variables in the condition are not mutated in the loop body
11-
--> $DIR/infinite_loop.rs:25:11
11+
--> $DIR/infinite_loop.rs:27:11
1212
|
1313
LL | while y < 10 && x < 3 {
1414
| ^^^^^^^^^^^^^^^
1515
|
1616
= note: this may lead to an infinite or to a never running loop
1717

1818
error: variables in the condition are not mutated in the loop body
19-
--> $DIR/infinite_loop.rs:32:11
19+
--> $DIR/infinite_loop.rs:34:11
2020
|
2121
LL | while !cond {
2222
| ^^^^^
2323
|
2424
= note: this may lead to an infinite or to a never running loop
2525

2626
error: variables in the condition are not mutated in the loop body
27-
--> $DIR/infinite_loop.rs:76:11
27+
--> $DIR/infinite_loop.rs:78:11
2828
|
2929
LL | while i < 3 {
3030
| ^^^^^
3131
|
3232
= note: this may lead to an infinite or to a never running loop
3333

3434
error: variables in the condition are not mutated in the loop body
35-
--> $DIR/infinite_loop.rs:81:11
35+
--> $DIR/infinite_loop.rs:83:11
3636
|
3737
LL | while i < 3 && j > 0 {
3838
| ^^^^^^^^^^^^^^
3939
|
4040
= note: this may lead to an infinite or to a never running loop
4141

4242
error: variables in the condition are not mutated in the loop body
43-
--> $DIR/infinite_loop.rs:85:11
43+
--> $DIR/infinite_loop.rs:87:11
4444
|
4545
LL | while i < 3 {
4646
| ^^^^^
4747
|
4848
= note: this may lead to an infinite or to a never running loop
4949

5050
error: variables in the condition are not mutated in the loop body
51-
--> $DIR/infinite_loop.rs:100:11
51+
--> $DIR/infinite_loop.rs:102:11
5252
|
5353
LL | while i < 3 {
5454
| ^^^^^
5555
|
5656
= note: this may lead to an infinite or to a never running loop
5757

5858
error: variables in the condition are not mutated in the loop body
59-
--> $DIR/infinite_loop.rs:105:11
59+
--> $DIR/infinite_loop.rs:107:11
6060
|
6161
LL | while i < 3 {
6262
| ^^^^^
6363
|
6464
= note: this may lead to an infinite or to a never running loop
6565

6666
error: variables in the condition are not mutated in the loop body
67-
--> $DIR/infinite_loop.rs:171:15
67+
--> $DIR/infinite_loop.rs:173:15
6868
|
6969
LL | while self.count < n {
7070
| ^^^^^^^^^^^^^^
7171
|
7272
= note: this may lead to an infinite or to a never running loop
7373

7474
error: variables in the condition are not mutated in the loop body
75-
--> $DIR/infinite_loop.rs:179:11
75+
--> $DIR/infinite_loop.rs:181:11
7676
|
7777
LL | while y < 10 {
7878
| ^^^^^^
@@ -82,7 +82,7 @@ LL | while y < 10 {
8282
= help: rewrite it as `if cond { loop { } }`
8383

8484
error: variables in the condition are not mutated in the loop body
85-
--> $DIR/infinite_loop.rs:186:11
85+
--> $DIR/infinite_loop.rs:188:11
8686
|
8787
LL | while y < 10 {
8888
| ^^^^^^
@@ -91,13 +91,5 @@ LL | while y < 10 {
9191
= note: this loop contains `return`s or `break`s
9292
= help: rewrite it as `if cond { loop { } }`
9393

94-
error: this argument is a mutable reference, but not used mutably
95-
--> $DIR/infinite_loop.rs:7:17
96-
|
97-
LL | fn fn_mutref(i: &mut i32) {
98-
| ^^^^^^^^ help: consider changing to: `&i32`
99-
|
100-
= note: `-D clippy::needless-pass-by-ref-mut` implied by `-D warnings`
101-
102-
error: aborting due to 12 previous errors
94+
error: aborting due to 11 previous errors
10395

tests/ui/let_underscore_future.rs

Lines changed: 2 additions & 0 deletions
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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: non-binding `let` on a future
2-
--> $DIR/let_underscore_future.rs:14:5
2+
--> $DIR/let_underscore_future.rs:16:5
33
|
44
LL | let _ = some_async_fn();
55
| ^^^^^^^^^^^^^^^^^^^^^^^^
@@ -8,28 +8,20 @@ LL | let _ = some_async_fn();
88
= note: `-D clippy::let-underscore-future` implied by `-D warnings`
99

1010
error: non-binding `let` on a future
11-
--> $DIR/let_underscore_future.rs:15:5
11+
--> $DIR/let_underscore_future.rs:17:5
1212
|
1313
LL | let _ = custom();
1414
| ^^^^^^^^^^^^^^^^^
1515
|
1616
= help: consider awaiting the future or dropping explicitly with `std::mem::drop`
1717

1818
error: non-binding `let` on a future
19-
--> $DIR/let_underscore_future.rs:19:5
19+
--> $DIR/let_underscore_future.rs:21:5
2020
|
2121
LL | let _ = future;
2222
| ^^^^^^^^^^^^^^^
2323
|
2424
= help: consider awaiting the future or dropping explicitly with `std::mem::drop`
2525

26-
error: this argument is a mutable reference, but not used mutably
27-
--> $DIR/let_underscore_future.rs:11:35
28-
|
29-
LL | fn do_something_to_future(future: &mut impl Future<Output = ()>) {}
30-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing to: `&impl Future<Output = ()>`
31-
|
32-
= note: `-D clippy::needless-pass-by-ref-mut` implied by `-D warnings`
33-
34-
error: aborting due to 4 previous errors
26+
error: aborting due to 3 previous errors
3527

tests/ui/mut_key.rs

Lines changed: 2 additions & 0 deletions
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

Lines changed: 18 additions & 26 deletions
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

1515
error: mutable key type
16-
--> $DIR/mut_key.rs:32:5
16+
--> $DIR/mut_key.rs:34:5
1717
|
1818
LL | let _other: HashMap<Key, bool> = HashMap::new();
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2020

2121
error: mutable key type
22-
--> $DIR/mut_key.rs:59:22
22+
--> $DIR/mut_key.rs:61:22
2323
|
2424
LL | fn tuples_bad<U>(_m: &mut HashMap<(Key, U), bool>) {}
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2626

2727
error: mutable key type
28-
--> $DIR/mut_key.rs:71:5
28+
--> $DIR/mut_key.rs:73:5
2929
|
3030
LL | let _map = HashMap::<Cell<usize>, usize>::new();
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3232

3333
error: mutable key type
34-
--> $DIR/mut_key.rs:72:5
34+
--> $DIR/mut_key.rs:74:5
3535
|
3636
LL | let _map = HashMap::<&mut Cell<usize>, usize>::new();
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3838

3939
error: mutable key type
40-
--> $DIR/mut_key.rs:73:5
40+
--> $DIR/mut_key.rs:75:5
4141
|
4242
LL | let _map = HashMap::<&mut usize, usize>::new();
4343
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4444

4545
error: mutable key type
46-
--> $DIR/mut_key.rs:75:5
46+
--> $DIR/mut_key.rs:77:5
4747
|
4848
LL | let _map = HashMap::<Vec<Cell<usize>>, usize>::new();
4949
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5050

5151
error: mutable key type
52-
--> $DIR/mut_key.rs:76:5
52+
--> $DIR/mut_key.rs:78:5
5353
|
5454
LL | let _map = HashMap::<BTreeMap<Cell<usize>, ()>, usize>::new();
5555
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5656

5757
error: mutable key type
58-
--> $DIR/mut_key.rs:77:5
58+
--> $DIR/mut_key.rs:79:5
5959
|
6060
LL | let _map = HashMap::<BTreeMap<(), Cell<usize>>, usize>::new();
6161
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6262

6363
error: mutable key type
64-
--> $DIR/mut_key.rs:78:5
64+
--> $DIR/mut_key.rs:80:5
6565
|
6666
LL | let _map = HashMap::<BTreeSet<Cell<usize>>, usize>::new();
6767
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6868

6969
error: mutable key type
70-
--> $DIR/mut_key.rs:79:5
70+
--> $DIR/mut_key.rs:81:5
7171
|
7272
LL | let _map = HashMap::<Option<Cell<usize>>, usize>::new();
7373
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7474

7575
error: mutable key type
76-
--> $DIR/mut_key.rs:80:5
76+
--> $DIR/mut_key.rs:82:5
7777
|
7878
LL | let _map = HashMap::<Option<Vec<Cell<usize>>>, usize>::new();
7979
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8080

8181
error: mutable key type
82-
--> $DIR/mut_key.rs:81:5
82+
--> $DIR/mut_key.rs:83:5
8383
|
8484
LL | let _map = HashMap::<Result<&mut usize, ()>, usize>::new();
8585
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8686

8787
error: mutable key type
88-
--> $DIR/mut_key.rs:83:5
88+
--> $DIR/mut_key.rs:85:5
8989
|
9090
LL | let _map = HashMap::<Box<Cell<usize>>, usize>::new();
9191
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9292

9393
error: mutable key type
94-
--> $DIR/mut_key.rs:84:5
94+
--> $DIR/mut_key.rs:86:5
9595
|
9696
LL | let _map = HashMap::<Rc<Cell<usize>>, usize>::new();
9797
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9898

9999
error: mutable key type
100-
--> $DIR/mut_key.rs:85:5
100+
--> $DIR/mut_key.rs:87:5
101101
|
102102
LL | let _map = HashMap::<Arc<Cell<usize>>, usize>::new();
103103
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
104104

105-
error: this argument is a mutable reference, but not used mutably
106-
--> $DIR/mut_key.rs:31:32
107-
|
108-
LL | fn should_not_take_this_arg(m: &mut HashMap<Key, usize>, _n: usize) -> HashSet<Key> {
109-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing to: `&HashMap<Key, usize>`
110-
|
111-
= note: `-D clippy::needless-pass-by-ref-mut` implied by `-D warnings`
112-
113-
error: aborting due to 18 previous errors
105+
error: aborting due to 17 previous errors
114106

tests/ui/mut_reference.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![allow(unused_variables, dead_code)]
1+
#![allow(clippy::needless_pass_by_ref_mut, unused_variables, dead_code)]
22

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

tests/ui/mut_reference.stderr

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,5 @@ error: the method `takes_an_immutable_reference` doesn't need a mutable referenc
1818
LL | my_struct.takes_an_immutable_reference(&mut 42);
1919
| ^^^^^^^
2020

21-
error: this argument is a mutable reference, but not used mutably
22-
--> $DIR/mut_reference.rs:24:44
23-
|
24-
LL | fn takes_a_mutable_reference(&self, a: &mut i32) {}
25-
| ^^^^^^^^ help: consider changing to: `&i32`
26-
|
27-
= note: `-D clippy::needless-pass-by-ref-mut` implied by `-D warnings`
28-
29-
error: aborting due to 4 previous errors
21+
error: aborting due to 3 previous errors
3022

0 commit comments

Comments
 (0)