about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-08-12 07:47:07 +0000
committerbors <bors@rust-lang.org>2021-08-12 07:47:07 +0000
commitdd9fe5ceab040e77452a046f54dfb77f92527dc6 (patch)
tree0a25bc641944702dbd88db417c3227a702ec3843
parentb1b38604f23b3a51620e2f9454353eb2fbf028a3 (diff)
parent979ce29086fb6bde6ac88700e1b4072d68739564 (diff)
downloadrust-dd9fe5ceab040e77452a046f54dfb77f92527dc6.tar.gz
rust-dd9fe5ceab040e77452a046f54dfb77f92527dc6.zip
Auto merge of #7556 - F3real:no_effect_inclusive_range, r=flip1995
No effect inclusive range

I noticed during last PR that range expression is `ExprKind::Struct` while inclusive range is `ExprKind::Call` which was why it was not handled. This PR adds check for this case.

changelog: [`no_effect]` Report inclusive range in no_effect lint
-rw-r--r--clippy_lints/src/no_effect.rs16
-rw-r--r--tests/ui/no_effect.stderr8
2 files changed, 16 insertions, 8 deletions
diff --git a/clippy_lints/src/no_effect.rs b/clippy_lints/src/no_effect.rs
index e07518b2586..28e9e6f438e 100644
--- a/clippy_lints/src/no_effect.rs
+++ b/clippy_lints/src/no_effect.rs
@@ -3,7 +3,7 @@ use clippy_utils::source::snippet_opt;
 use clippy_utils::ty::has_drop;
 use rustc_errors::Applicability;
 use rustc_hir::def::{DefKind, Res};
-use rustc_hir::{BinOpKind, BlockCheckMode, Expr, ExprKind, Stmt, StmtKind, UnsafeSource};
+use rustc_hir::{is_range_literal, BinOpKind, BlockCheckMode, Expr, ExprKind, Stmt, StmtKind, UnsafeSource};
 use rustc_lint::{LateContext, LateLintPass};
 use rustc_session::{declare_lint_pass, declare_tool_lint};
 use std::ops::Deref;
@@ -68,12 +68,14 @@ fn has_no_effect(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
         ExprKind::Call(callee, args) => {
             if let ExprKind::Path(ref qpath) = callee.kind {
                 let res = cx.qpath_res(qpath, callee.hir_id);
-                match res {
-                    Res::Def(DefKind::Struct | DefKind::Variant | DefKind::Ctor(..), ..) => {
-                        !has_drop(cx, cx.typeck_results().expr_ty(expr))
-                            && args.iter().all(|arg| has_no_effect(cx, arg))
-                    },
-                    _ => false,
+                let def_matched = matches!(
+                    res,
+                    Res::Def(DefKind::Struct | DefKind::Variant | DefKind::Ctor(..), ..)
+                );
+                if def_matched || is_range_literal(expr) {
+                    !has_drop(cx, cx.typeck_results().expr_ty(expr)) && args.iter().all(|arg| has_no_effect(cx, arg))
+                } else {
+                    false
                 }
             } else {
                 false
diff --git a/tests/ui/no_effect.stderr b/tests/ui/no_effect.stderr
index 834b9056e31..6b24675ac2d 100644
--- a/tests/ui/no_effect.stderr
+++ b/tests/ui/no_effect.stderr
@@ -109,6 +109,12 @@ LL |     5..6;
    |     ^^^^^
 
 error: statement with no effect
+  --> $DIR/no_effect.rs:83:5
+   |
+LL |     5..=6;
+   |     ^^^^^^
+
+error: statement with no effect
   --> $DIR/no_effect.rs:84:5
    |
 LL |     [42, 55];
@@ -150,5 +156,5 @@ error: statement with no effect
 LL |     FooString { s: s };
    |     ^^^^^^^^^^^^^^^^^^^
 
-error: aborting due to 25 previous errors
+error: aborting due to 26 previous errors