Skip to content

Commit 3051773

Browse files
committed
Auto merge of #7453 - F3real:assume_function_calls_have_side_effect, r=flip1995
Don't report function calls as unnecessary operation if used in array index Attempts to fix: #7412 changelog: Don't report function calls used in indexing as unnecessary operation. [`unnecessary_operation`]
2 parents 87c6f32 + ede977c commit 3051773

File tree

3 files changed

+94
-59
lines changed

3 files changed

+94
-59
lines changed

clippy_lints/src/no_effect.rs

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -96,28 +96,63 @@ impl<'tcx> LateLintPass<'tcx> for NoEffect {
9696
if has_no_effect(cx, expr) {
9797
span_lint_hir(cx, NO_EFFECT, expr.hir_id, stmt.span, "statement with no effect");
9898
} else if let Some(reduced) = reduce_expression(cx, expr) {
99-
let mut snippet = String::new();
100-
for e in reduced {
99+
for e in &reduced {
101100
if e.span.from_expansion() {
102101
return;
103102
}
104-
if let Some(snip) = snippet_opt(cx, e.span) {
105-
snippet.push_str(&snip);
106-
snippet.push(';');
107-
} else {
108-
return;
103+
}
104+
if let ExprKind::Index(..) = &expr.kind {
105+
let snippet;
106+
if_chain! {
107+
if let Some(arr) = snippet_opt(cx, reduced[0].span);
108+
if let Some(func) = snippet_opt(cx, reduced[1].span);
109+
then {
110+
snippet = format!("assert!({}.len() > {});", &arr, &func);
111+
} else {
112+
return;
113+
}
114+
}
115+
span_lint_hir_and_then(
116+
cx,
117+
UNNECESSARY_OPERATION,
118+
expr.hir_id,
119+
stmt.span,
120+
"unnecessary operation",
121+
|diag| {
122+
diag.span_suggestion(
123+
stmt.span,
124+
"statement can be written as",
125+
snippet,
126+
Applicability::MaybeIncorrect,
127+
);
128+
},
129+
);
130+
} else {
131+
let mut snippet = String::new();
132+
for e in reduced {
133+
if let Some(snip) = snippet_opt(cx, e.span) {
134+
snippet.push_str(&snip);
135+
snippet.push(';');
136+
} else {
137+
return;
138+
}
109139
}
140+
span_lint_hir_and_then(
141+
cx,
142+
UNNECESSARY_OPERATION,
143+
expr.hir_id,
144+
stmt.span,
145+
"unnecessary operation",
146+
|diag| {
147+
diag.span_suggestion(
148+
stmt.span,
149+
"statement can be reduced to",
150+
snippet,
151+
Applicability::MachineApplicable,
152+
);
153+
},
154+
);
110155
}
111-
span_lint_hir_and_then(
112-
cx,
113-
UNNECESSARY_OPERATION,
114-
expr.hir_id,
115-
stmt.span,
116-
"statement can be reduced",
117-
|diag| {
118-
diag.span_suggestion(stmt.span, "replace it with", snippet, Applicability::MachineApplicable);
119-
},
120-
);
121156
}
122157
}
123158
}

tests/ui/unnecessary_operation.fixed

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ fn main() {
6262
get_number();
6363
5;get_number();
6464
42;get_number();
65-
[42, 55];get_usize();
65+
assert!([42, 55].len() > get_usize());
6666
42;get_number();
6767
get_number();
68-
[42; 55];get_usize();
68+
assert!([42; 55].len() > get_usize());
6969
get_number();
7070
String::from("blah");
7171

tests/ui/unnecessary_operation.stderr

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,128 @@
1-
error: statement can be reduced
1+
error: unnecessary operation
22
--> $DIR/unnecessary_operation.rs:51:5
33
|
44
LL | Tuple(get_number());
5-
| ^^^^^^^^^^^^^^^^^^^^ help: replace it with: `get_number();`
5+
| ^^^^^^^^^^^^^^^^^^^^ help: statement can be reduced to: `get_number();`
66
|
77
= note: `-D clippy::unnecessary-operation` implied by `-D warnings`
88

9-
error: statement can be reduced
9+
error: unnecessary operation
1010
--> $DIR/unnecessary_operation.rs:52:5
1111
|
1212
LL | Struct { field: get_number() };
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `get_number();`
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: statement can be reduced to: `get_number();`
1414

15-
error: statement can be reduced
15+
error: unnecessary operation
1616
--> $DIR/unnecessary_operation.rs:53:5
1717
|
1818
LL | Struct { ..get_struct() };
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `get_struct();`
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: statement can be reduced to: `get_struct();`
2020

21-
error: statement can be reduced
21+
error: unnecessary operation
2222
--> $DIR/unnecessary_operation.rs:54:5
2323
|
2424
LL | Enum::Tuple(get_number());
25-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `get_number();`
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: statement can be reduced to: `get_number();`
2626

27-
error: statement can be reduced
27+
error: unnecessary operation
2828
--> $DIR/unnecessary_operation.rs:55:5
2929
|
3030
LL | Enum::Struct { field: get_number() };
31-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `get_number();`
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: statement can be reduced to: `get_number();`
3232

33-
error: statement can be reduced
33+
error: unnecessary operation
3434
--> $DIR/unnecessary_operation.rs:56:5
3535
|
3636
LL | 5 + get_number();
37-
| ^^^^^^^^^^^^^^^^^ help: replace it with: `5;get_number();`
37+
| ^^^^^^^^^^^^^^^^^ help: statement can be reduced to: `5;get_number();`
3838

39-
error: statement can be reduced
39+
error: unnecessary operation
4040
--> $DIR/unnecessary_operation.rs:57:5
4141
|
4242
LL | *&get_number();
43-
| ^^^^^^^^^^^^^^^ help: replace it with: `get_number();`
43+
| ^^^^^^^^^^^^^^^ help: statement can be reduced to: `get_number();`
4444

45-
error: statement can be reduced
45+
error: unnecessary operation
4646
--> $DIR/unnecessary_operation.rs:58:5
4747
|
4848
LL | &get_number();
49-
| ^^^^^^^^^^^^^^ help: replace it with: `get_number();`
49+
| ^^^^^^^^^^^^^^ help: statement can be reduced to: `get_number();`
5050

51-
error: statement can be reduced
51+
error: unnecessary operation
5252
--> $DIR/unnecessary_operation.rs:59:5
5353
|
5454
LL | (5, 6, get_number());
55-
| ^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `5;6;get_number();`
55+
| ^^^^^^^^^^^^^^^^^^^^^ help: statement can be reduced to: `5;6;get_number();`
5656

57-
error: statement can be reduced
57+
error: unnecessary operation
5858
--> $DIR/unnecessary_operation.rs:60:5
5959
|
6060
LL | box get_number();
61-
| ^^^^^^^^^^^^^^^^^ help: replace it with: `get_number();`
61+
| ^^^^^^^^^^^^^^^^^ help: statement can be reduced to: `get_number();`
6262

63-
error: statement can be reduced
63+
error: unnecessary operation
6464
--> $DIR/unnecessary_operation.rs:61:5
6565
|
6666
LL | get_number()..;
67-
| ^^^^^^^^^^^^^^^ help: replace it with: `get_number();`
67+
| ^^^^^^^^^^^^^^^ help: statement can be reduced to: `get_number();`
6868

69-
error: statement can be reduced
69+
error: unnecessary operation
7070
--> $DIR/unnecessary_operation.rs:62:5
7171
|
7272
LL | ..get_number();
73-
| ^^^^^^^^^^^^^^^ help: replace it with: `get_number();`
73+
| ^^^^^^^^^^^^^^^ help: statement can be reduced to: `get_number();`
7474

75-
error: statement can be reduced
75+
error: unnecessary operation
7676
--> $DIR/unnecessary_operation.rs:63:5
7777
|
7878
LL | 5..get_number();
79-
| ^^^^^^^^^^^^^^^^ help: replace it with: `5;get_number();`
79+
| ^^^^^^^^^^^^^^^^ help: statement can be reduced to: `5;get_number();`
8080

81-
error: statement can be reduced
81+
error: unnecessary operation
8282
--> $DIR/unnecessary_operation.rs:64:5
8383
|
8484
LL | [42, get_number()];
85-
| ^^^^^^^^^^^^^^^^^^^ help: replace it with: `42;get_number();`
85+
| ^^^^^^^^^^^^^^^^^^^ help: statement can be reduced to: `42;get_number();`
8686

87-
error: statement can be reduced
87+
error: unnecessary operation
8888
--> $DIR/unnecessary_operation.rs:65:5
8989
|
9090
LL | [42, 55][get_usize()];
91-
| ^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `[42, 55];get_usize();`
91+
| ^^^^^^^^^^^^^^^^^^^^^^ help: statement can be written as: `assert!([42, 55].len() > get_usize());`
9292

93-
error: statement can be reduced
93+
error: unnecessary operation
9494
--> $DIR/unnecessary_operation.rs:66:5
9595
|
9696
LL | (42, get_number()).1;
97-
| ^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `42;get_number();`
97+
| ^^^^^^^^^^^^^^^^^^^^^ help: statement can be reduced to: `42;get_number();`
9898

99-
error: statement can be reduced
99+
error: unnecessary operation
100100
--> $DIR/unnecessary_operation.rs:67:5
101101
|
102102
LL | [get_number(); 55];
103-
| ^^^^^^^^^^^^^^^^^^^ help: replace it with: `get_number();`
103+
| ^^^^^^^^^^^^^^^^^^^ help: statement can be reduced to: `get_number();`
104104

105-
error: statement can be reduced
105+
error: unnecessary operation
106106
--> $DIR/unnecessary_operation.rs:68:5
107107
|
108108
LL | [42; 55][get_usize()];
109-
| ^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `[42; 55];get_usize();`
109+
| ^^^^^^^^^^^^^^^^^^^^^^ help: statement can be written as: `assert!([42; 55].len() > get_usize());`
110110

111-
error: statement can be reduced
111+
error: unnecessary operation
112112
--> $DIR/unnecessary_operation.rs:69:5
113113
|
114114
LL | / {
115115
LL | | get_number()
116116
LL | | };
117-
| |______^ help: replace it with: `get_number();`
117+
| |______^ help: statement can be reduced to: `get_number();`
118118

119-
error: statement can be reduced
119+
error: unnecessary operation
120120
--> $DIR/unnecessary_operation.rs:72:5
121121
|
122122
LL | / FooString {
123123
LL | | s: String::from("blah"),
124124
LL | | };
125-
| |______^ help: replace it with: `String::from("blah");`
125+
| |______^ help: statement can be reduced to: `String::from("blah");`
126126

127127
error: aborting due to 20 previous errors
128128

0 commit comments

Comments
 (0)