Skip to content

Commit ef24685

Browse files
committed
Don't shadow unnecessary operation lint if no_effect is allowed
1 parent f18a2f1 commit ef24685

26 files changed

+316
-60
lines changed

clippy_lints/src/no_effect.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_utils::diagnostics::{span_lint_hir, span_lint_hir_and_then};
2+
use clippy_utils::is_lint_allowed;
23
use clippy_utils::source::snippet_opt;
34
use clippy_utils::ty::has_drop;
45
use rustc_errors::Applicability;
@@ -79,12 +80,13 @@ impl<'tcx> LateLintPass<'tcx> for NoEffect {
7980

8081
fn check_no_effect(cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) -> bool {
8182
if let StmtKind::Semi(expr) = stmt.kind {
82-
if has_no_effect(cx, expr) {
83+
if has_no_effect(cx, expr) && !is_lint_allowed(cx, NO_EFFECT, expr.hir_id) {
8384
span_lint_hir(cx, NO_EFFECT, expr.hir_id, stmt.span, "statement with no effect");
8485
return true;
8586
}
8687
} else if let StmtKind::Local(local) = stmt.kind {
8788
if_chain! {
89+
if !is_lint_allowed(cx, NO_EFFECT_UNDERSCORE_BINDING, local.hir_id);
8890
if let Some(init) = local.init;
8991
if !local.pat.span.from_expansion();
9092
if has_no_effect(cx, init);

tests/ui-internal/if_chain_style.stderr

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ error: `let` expression should be inside `then { .. }`
3232
LL | let x = "";
3333
| ^^^^^^^^^^^
3434

35+
error: unnecessary operation
36+
--> $DIR/if_chain_style.rs:26:13
37+
|
38+
LL | ();
39+
| ^^^ help: statement can be reduced to
40+
|
41+
= note: `-D clippy::unnecessary-operation` implied by `-D warnings`
42+
3543
error: this `if` can be part of the outer `if_chain!`
3644
--> $DIR/if_chain_style.rs:35:13
3745
|
@@ -81,5 +89,23 @@ LL | / let x = "";
8189
LL | | let x = "";
8290
| |_______________________^
8391

84-
error: aborting due to 7 previous errors
92+
error: unnecessary operation
93+
--> $DIR/if_chain_style.rs:57:9
94+
|
95+
LL | ();
96+
| ^^^ help: statement can be reduced to
97+
98+
error: unnecessary operation
99+
--> $DIR/if_chain_style.rs:61:20
100+
|
101+
LL | then { (); }
102+
| ^^^ help: statement can be reduced to
103+
104+
error: unnecessary operation
105+
--> $DIR/if_chain_style.rs:68:16
106+
|
107+
LL | then { (); }
108+
| ^^^ help: statement can be reduced to
109+
110+
error: aborting due to 11 previous errors
85111

tests/ui/author/blocks.rs

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

33
#[rustfmt::skip]
44
fn main() {

tests/ui/cfg_attr_rustfmt.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// run-rustfix
22
#![feature(stmt_expr_attributes)]
33

4-
#![allow(unused, clippy::no_effect)]
4+
#![allow(unused, clippy::no_effect, clippy::unnecessary_operation)]
55
#![warn(clippy::deprecated_cfg_attr)]
66

77
// This doesn't get linted, see known problems

tests/ui/cfg_attr_rustfmt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// run-rustfix
22
#![feature(stmt_expr_attributes)]
33

4-
#![allow(unused, clippy::no_effect)]
4+
#![allow(unused, clippy::no_effect, clippy::unnecessary_operation)]
55
#![warn(clippy::deprecated_cfg_attr)]
66

77
// This doesn't get linted, see known problems

tests/ui/crashes/ice-7340.rs

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

33
fn main() {
44
const CONSTANT: usize = 8;

tests/ui/erasing_op.stderr

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
error: unnecessary operation
2+
--> $DIR/erasing_op.rs:6:5
3+
|
4+
LL | x * 0;
5+
| ^^^^^^ help: statement can be reduced to: `x;0;`
6+
|
7+
= note: `-D clippy::unnecessary-operation` implied by `-D warnings`
8+
19
error: this operation will always return zero. This is likely not the intended outcome
210
--> $DIR/erasing_op.rs:6:5
311
|
@@ -6,17 +14,29 @@ LL | x * 0;
614
|
715
= note: `-D clippy::erasing-op` implied by `-D warnings`
816

17+
error: unnecessary operation
18+
--> $DIR/erasing_op.rs:7:5
19+
|
20+
LL | 0 & x;
21+
| ^^^^^^ help: statement can be reduced to: `0;x;`
22+
923
error: this operation will always return zero. This is likely not the intended outcome
1024
--> $DIR/erasing_op.rs:7:5
1125
|
1226
LL | 0 & x;
1327
| ^^^^^
1428

29+
error: unnecessary operation
30+
--> $DIR/erasing_op.rs:8:5
31+
|
32+
LL | 0 / x;
33+
| ^^^^^^ help: statement can be reduced to: `0;x;`
34+
1535
error: this operation will always return zero. This is likely not the intended outcome
1636
--> $DIR/erasing_op.rs:8:5
1737
|
1838
LL | 0 / x;
1939
| ^^^^^
2040

21-
error: aborting due to 3 previous errors
41+
error: aborting due to 6 previous errors
2242

tests/ui/eta.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ macro_rules! closure_mac {
2929

3030
fn main() {
3131
let a = Some(1u8).map(foo);
32-
let c = Some(1u8).map(|a| {1+2; foo}(a));
32+
let c = Some(1u8).map(|a| {1;2; foo}(a));
3333
true.then(|| mac!()); // don't lint function in macro expansion
3434
Some(1).map(closure_mac!()); // don't lint closure in macro expansion
3535
let _: Option<Vec<u8>> = true.then(std::vec::Vec::new); // special case vec!

tests/ui/eta.stderr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ LL | let a = Some(1u8).map(|a| foo(a));
66
|
77
= note: `-D clippy::redundant-closure` implied by `-D warnings`
88

9+
error: unnecessary operation
10+
--> $DIR/eta.rs:32:32
11+
|
12+
LL | let c = Some(1u8).map(|a| {1+2; foo}(a));
13+
| ^^^^ help: statement can be reduced to: `1;2;`
14+
|
15+
= note: `-D clippy::unnecessary-operation` implied by `-D warnings`
16+
917
error: redundant closure
1018
--> $DIR/eta.rs:35:40
1119
|
@@ -130,5 +138,5 @@ error: redundant closure
130138
LL | map_str_to_path(|s| s.as_ref());
131139
| ^^^^^^^^^^^^^^ help: replace the closure with the method itself: `std::convert::AsRef::as_ref`
132140

133-
error: aborting due to 21 previous errors
141+
error: aborting due to 22 previous errors
134142

tests/ui/if_same_then_else.stderr

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,92 @@ LL | | foo();
2525
LL | | }
2626
| |_____^
2727

28+
error: unnecessary operation
29+
--> $DIR/if_same_then_else.rs:22:9
30+
|
31+
LL | Foo { bar: 42 };
32+
| ^^^^^^^^^^^^^^^^ help: statement can be reduced to: `42;`
33+
|
34+
= note: `-D clippy::unnecessary-operation` implied by `-D warnings`
35+
36+
error: unnecessary operation
37+
--> $DIR/if_same_then_else.rs:23:9
38+
|
39+
LL | 0..10;
40+
| ^^^^^^ help: statement can be reduced to: `0;10;`
41+
42+
error: unnecessary operation
43+
--> $DIR/if_same_then_else.rs:24:9
44+
|
45+
LL | ..;
46+
| ^^^ help: statement can be reduced to
47+
48+
error: unnecessary operation
49+
--> $DIR/if_same_then_else.rs:25:9
50+
|
51+
LL | 0..;
52+
| ^^^^ help: statement can be reduced to: `0;`
53+
54+
error: unnecessary operation
55+
--> $DIR/if_same_then_else.rs:26:9
56+
|
57+
LL | ..10;
58+
| ^^^^^ help: statement can be reduced to: `10;`
59+
60+
error: unnecessary operation
61+
--> $DIR/if_same_then_else.rs:31:9
62+
|
63+
LL | Foo { bar: 42 };
64+
| ^^^^^^^^^^^^^^^^ help: statement can be reduced to: `42;`
65+
66+
error: unnecessary operation
67+
--> $DIR/if_same_then_else.rs:32:9
68+
|
69+
LL | 0..10;
70+
| ^^^^^^ help: statement can be reduced to: `0;10;`
71+
72+
error: unnecessary operation
73+
--> $DIR/if_same_then_else.rs:33:9
74+
|
75+
LL | ..;
76+
| ^^^ help: statement can be reduced to
77+
78+
error: unnecessary operation
79+
--> $DIR/if_same_then_else.rs:34:9
80+
|
81+
LL | 0..;
82+
| ^^^^ help: statement can be reduced to: `0;`
83+
84+
error: unnecessary operation
85+
--> $DIR/if_same_then_else.rs:35:9
86+
|
87+
LL | ..10;
88+
| ^^^^^ help: statement can be reduced to: `10;`
89+
90+
error: unnecessary operation
91+
--> $DIR/if_same_then_else.rs:41:9
92+
|
93+
LL | Foo { bar: 42 };
94+
| ^^^^^^^^^^^^^^^^ help: statement can be reduced to: `42;`
95+
96+
error: unnecessary operation
97+
--> $DIR/if_same_then_else.rs:43:9
98+
|
99+
LL | Foo { bar: 43 };
100+
| ^^^^^^^^^^^^^^^^ help: statement can be reduced to: `43;`
101+
102+
error: unnecessary operation
103+
--> $DIR/if_same_then_else.rs:47:9
104+
|
105+
LL | ();
106+
| ^^^ help: statement can be reduced to
107+
108+
error: unnecessary operation
109+
--> $DIR/if_same_then_else.rs:53:9
110+
|
111+
LL | 0..10;
112+
| ^^^^^^ help: statement can be reduced to: `0;10;`
113+
28114
error: this `if` has identical blocks
29115
--> $DIR/if_same_then_else.rs:65:21
30116
|
@@ -108,5 +194,17 @@ LL | | bar + 1;
108194
LL | | }
109195
| |_____^
110196

111-
error: aborting due to 5 previous errors
197+
error: unnecessary operation
198+
--> $DIR/if_same_then_else.rs:101:9
199+
|
200+
LL | bar + 1;
201+
| ^^^^^^^^ help: statement can be reduced to: `bar;1;`
202+
203+
error: unnecessary operation
204+
--> $DIR/if_same_then_else.rs:109:9
205+
|
206+
LL | bar + 1;
207+
| ^^^^^^^^ help: statement can be reduced to: `bar;1;`
208+
209+
error: aborting due to 21 previous errors
112210

0 commit comments

Comments
 (0)