From 1303c76da43ae2c1798917a32c52df50275ca422 Mon Sep 17 00:00:00 2001 From: F3real Date: Mon, 9 Aug 2021 09:49:55 +0200 Subject: [PATCH] Update suggestion in case function call is used in index --- clippy_lints/src/no_effect.rs | 76 +++++++++++++++++++++------ tests/ui/unnecessary_operation.fixed | 4 +- tests/ui/unnecessary_operation.stderr | 4 +- 3 files changed, 63 insertions(+), 21 deletions(-) diff --git a/clippy_lints/src/no_effect.rs b/clippy_lints/src/no_effect.rs index e07518b25868..b3bdef89a35c 100644 --- a/clippy_lints/src/no_effect.rs +++ b/clippy_lints/src/no_effect.rs @@ -94,33 +94,75 @@ 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>> { if expr.span.from_expansion() { return None; diff --git a/tests/ui/unnecessary_operation.fixed b/tests/ui/unnecessary_operation.fixed index 2fca96c4cd55..bf0ec8deb345 100644 --- a/tests/ui/unnecessary_operation.fixed +++ b/tests/ui/unnecessary_operation.fixed @@ -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"); diff --git a/tests/ui/unnecessary_operation.stderr b/tests/ui/unnecessary_operation.stderr index f88c9f9908be..a97bc167e2e7 100644 --- a/tests/ui/unnecessary_operation.stderr +++ b/tests/ui/unnecessary_operation.stderr @@ -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 @@ -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