Skip to content

Commit dd9fe5c

Browse files
committed
Auto merge of rust-lang#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
2 parents b1b3860 + 979ce29 commit dd9fe5c

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

clippy_lints/src/no_effect.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::source::snippet_opt;
33
use clippy_utils::ty::has_drop;
44
use rustc_errors::Applicability;
55
use rustc_hir::def::{DefKind, Res};
6-
use rustc_hir::{BinOpKind, BlockCheckMode, Expr, ExprKind, Stmt, StmtKind, UnsafeSource};
6+
use rustc_hir::{is_range_literal, BinOpKind, BlockCheckMode, Expr, ExprKind, Stmt, StmtKind, UnsafeSource};
77
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_session::{declare_lint_pass, declare_tool_lint};
99
use std::ops::Deref;
@@ -68,12 +68,14 @@ fn has_no_effect(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
6868
ExprKind::Call(callee, args) => {
6969
if let ExprKind::Path(ref qpath) = callee.kind {
7070
let res = cx.qpath_res(qpath, callee.hir_id);
71-
match res {
72-
Res::Def(DefKind::Struct | DefKind::Variant | DefKind::Ctor(..), ..) => {
73-
!has_drop(cx, cx.typeck_results().expr_ty(expr))
74-
&& args.iter().all(|arg| has_no_effect(cx, arg))
75-
},
76-
_ => false,
71+
let def_matched = matches!(
72+
res,
73+
Res::Def(DefKind::Struct | DefKind::Variant | DefKind::Ctor(..), ..)
74+
);
75+
if def_matched || is_range_literal(expr) {
76+
!has_drop(cx, cx.typeck_results().expr_ty(expr)) && args.iter().all(|arg| has_no_effect(cx, arg))
77+
} else {
78+
false
7779
}
7880
} else {
7981
false

tests/ui/no_effect.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ error: statement with no effect
108108
LL | 5..6;
109109
| ^^^^^
110110

111+
error: statement with no effect
112+
--> $DIR/no_effect.rs:83:5
113+
|
114+
LL | 5..=6;
115+
| ^^^^^^
116+
111117
error: statement with no effect
112118
--> $DIR/no_effect.rs:84:5
113119
|
@@ -150,5 +156,5 @@ error: statement with no effect
150156
LL | FooString { s: s };
151157
| ^^^^^^^^^^^^^^^^^^^
152158

153-
error: aborting due to 25 previous errors
159+
error: aborting due to 26 previous errors
154160

0 commit comments

Comments
 (0)