1
Fork 0

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
This commit is contained in:
bors 2021-08-12 07:47:07 +00:00
commit dd9fe5ceab
2 changed files with 16 additions and 8 deletions

View file

@ -3,7 +3,7 @@ use clippy_utils::source::snippet_opt;
use clippy_utils::ty::has_drop; use clippy_utils::ty::has_drop;
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::def::{DefKind, Res}; 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_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
use std::ops::Deref; use std::ops::Deref;
@ -68,12 +68,14 @@ fn has_no_effect(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
ExprKind::Call(callee, args) => { ExprKind::Call(callee, args) => {
if let ExprKind::Path(ref qpath) = callee.kind { if let ExprKind::Path(ref qpath) = callee.kind {
let res = cx.qpath_res(qpath, callee.hir_id); let res = cx.qpath_res(qpath, callee.hir_id);
match res { let def_matched = matches!(
Res::Def(DefKind::Struct | DefKind::Variant | DefKind::Ctor(..), ..) => { res,
!has_drop(cx, cx.typeck_results().expr_ty(expr)) Res::Def(DefKind::Struct | DefKind::Variant | DefKind::Ctor(..), ..)
&& args.iter().all(|arg| has_no_effect(cx, arg)) );
}, if def_matched || is_range_literal(expr) {
_ => false, !has_drop(cx, cx.typeck_results().expr_ty(expr)) && args.iter().all(|arg| has_no_effect(cx, arg))
} else {
false
} }
} else { } else {
false false

View file

@ -108,6 +108,12 @@ error: statement with no effect
LL | 5..6; LL | 5..6;
| ^^^^^ | ^^^^^
error: statement with no effect
--> $DIR/no_effect.rs:83:5
|
LL | 5..=6;
| ^^^^^^
error: statement with no effect error: statement with no effect
--> $DIR/no_effect.rs:84:5 --> $DIR/no_effect.rs:84:5
| |
@ -150,5 +156,5 @@ error: statement with no effect
LL | FooString { s: s }; LL | FooString { s: s };
| ^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^
error: aborting due to 25 previous errors error: aborting due to 26 previous errors