Skip to content

Commit

Permalink
Update suggestion in case function call is used in index
Browse files Browse the repository at this point in the history
  • Loading branch information
F3real committed Aug 9, 2021
1 parent 2dbf0c1 commit 06b271d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 21 deletions.
71 changes: 54 additions & 17 deletions clippy_lints/src/no_effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,33 +94,70 @@ impl<'tcx> LateLintPass<'tcx> for NoEffect {
if has_no_effect(cx, expr) {
span_lint_hir(cx, NO_EFFECT, expr.hir_id, stmt.span, "statement with no effect");
} else if let Some(reduced) = reduce_expression(cx, expr) {
let mut snippet = String::new();
for e in reduced {
for e in &reduced {
if e.span.from_expansion() {
return;
}
if let Some(snip) = snippet_opt(cx, e.span) {
snippet.push_str(&snip);
snippet.push(';');
} else {
return;
}
if is_function_used_in_index(expr) {
let snippet;
if_chain!{
if let Some(arr) = snippet_opt(cx, reduced[0].span);
if let Some(func) = snippet_opt(cx, reduced[1].span);
then {
snippet = format!("assert!({}.len() > {});", &arr, &func);
} else {
return;
}
}
span_lint_hir_and_then(
cx,
UNNECESSARY_OPERATION,
expr.hir_id,
stmt.span,
"statement can be reduced",
|diag| {
diag.span_suggestion(stmt.span, "replace it with", snippet, Applicability::MaybeIncorrect);
},
);
} else {
let mut snippet = String::new();
for e in reduced {
if let Some(snip) = snippet_opt(cx, e.span) {
snippet.push_str(&snip);
snippet.push(';');
} else {
return;
}
}
span_lint_hir_and_then(
cx,
UNNECESSARY_OPERATION,
expr.hir_id,
stmt.span,
"statement can be reduced",
|diag| {
diag.span_suggestion(stmt.span, "replace it with", snippet, Applicability::MachineApplicable);
},
);
}
span_lint_hir_and_then(
cx,
UNNECESSARY_OPERATION,
expr.hir_id,
stmt.span,
"statement can be reduced",
|diag| {
diag.span_suggestion(stmt.span, "replace it with", snippet, Applicability::MachineApplicable);
},
);
}
}
}
}

fn is_function_used_in_index(expr: &Expr<'_>) -> bool {
if_chain! {
if let ExprKind::Index(_, b) = &expr.kind;
if let ExprKind::Call(..) | ExprKind::MethodCall(..) = &b.kind;
then {
true
} else {
false
}
}
}

fn reduce_expression<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<Vec<&'a Expr<'a>>> {
if expr.span.from_expansion() {
return None;
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/unnecessary_operation.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ fn main() {
get_number();
5;get_number();
42;get_number();
[42, 55];get_usize();
assert!([42, 55].len() > get_usize());
42;get_number();
get_number();
[42; 55];get_usize();
assert!([42; 55].len() > get_usize());
get_number();
String::from("blah");

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/unnecessary_operation.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ error: statement can be reduced
--> $DIR/unnecessary_operation.rs:65:5
|
LL | [42, 55][get_usize()];
| ^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `[42, 55];get_usize();`
| ^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!([42, 55].len() > get_usize());`

error: statement can be reduced
--> $DIR/unnecessary_operation.rs:66:5
Expand All @@ -106,7 +106,7 @@ error: statement can be reduced
--> $DIR/unnecessary_operation.rs:68:5
|
LL | [42; 55][get_usize()];
| ^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `[42; 55];get_usize();`
| ^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!([42; 55].len() > get_usize());`

error: statement can be reduced
--> $DIR/unnecessary_operation.rs:69:5
Expand Down

0 comments on commit 06b271d

Please sign in to comment.