Skip to content

Commit 4cda21d

Browse files
committed
Auto merge of #9981 - Jarcho:issue_9954, r=flip1995
Don't lint `unnecessary_operation` in mixed macro contexts fixes #9954 changelog: `unnecessary_operation`: Don't lint in mixed macro contexts.
2 parents e9a8b8c + 0893322 commit 4cda21d

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

clippy_lints/src/no_effect.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use clippy_utils::ty::has_drop;
66
use rustc_errors::Applicability;
77
use rustc_hir::def::{DefKind, Res};
88
use rustc_hir::{is_range_literal, BinOpKind, BlockCheckMode, Expr, ExprKind, PatKind, Stmt, StmtKind, UnsafeSource};
9-
use rustc_lint::{LateContext, LateLintPass};
9+
use rustc_lint::{LateContext, LateLintPass, LintContext};
10+
use rustc_middle::lint::in_external_macro;
1011
use rustc_session::{declare_lint_pass, declare_tool_lint};
1112
use std::ops::Deref;
1213

@@ -159,8 +160,11 @@ fn has_no_effect(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
159160
fn check_unnecessary_operation(cx: &LateContext<'_>, stmt: &Stmt<'_>) {
160161
if_chain! {
161162
if let StmtKind::Semi(expr) = stmt.kind;
163+
let ctxt = stmt.span.ctxt();
164+
if expr.span.ctxt() == ctxt;
162165
if let Some(reduced) = reduce_expression(cx, expr);
163-
if !&reduced.iter().any(|e| e.span.from_expansion());
166+
if !in_external_macro(cx.sess(), stmt.span);
167+
if reduced.iter().all(|e| e.span.ctxt() == ctxt);
164168
then {
165169
if let ExprKind::Index(..) = &expr.kind {
166170
let snippet = if let (Some(arr), Some(func)) =

tests/ui/unnecessary_operation.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,13 @@ fn main() {
7676
DropStruct { ..get_drop_struct() };
7777
DropEnum::Tuple(get_number());
7878
DropEnum::Struct { field: get_number() };
79+
80+
// Issue #9954
81+
fn one() -> i8 {
82+
1
83+
}
84+
macro_rules! use_expr {
85+
($($e:expr),*) => {{ $($e;)* }}
86+
}
87+
use_expr!(isize::MIN / -(one() as isize), i8::MIN / -one());
7988
}

tests/ui/unnecessary_operation.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,13 @@ fn main() {
8080
DropStruct { ..get_drop_struct() };
8181
DropEnum::Tuple(get_number());
8282
DropEnum::Struct { field: get_number() };
83+
84+
// Issue #9954
85+
fn one() -> i8 {
86+
1
87+
}
88+
macro_rules! use_expr {
89+
($($e:expr),*) => {{ $($e;)* }}
90+
}
91+
use_expr!(isize::MIN / -(one() as isize), i8::MIN / -one());
8392
}

0 commit comments

Comments
 (0)