Skip to content

Check if dropping an expression may have indirect side-effects #14594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions clippy_lints/src/no_effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,16 @@ fn is_operator_overridden(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
}
}

/// Checks if dropping `expr` might have a visible side effect.
fn expr_ty_has_significant_drop(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
let ty = cx.typeck_results().expr_ty(expr);
ty.has_significant_drop(cx.tcx, cx.typing_env())
}

fn has_no_effect(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
match expr.kind {
ExprKind::Lit(..) | ExprKind::Closure { .. } => true,
ExprKind::Path(..) => !has_drop(cx, cx.typeck_results().expr_ty(expr)),
ExprKind::Path(..) => !expr_ty_has_significant_drop(cx, expr),
ExprKind::Index(a, b, _) | ExprKind::Binary(_, a, b) => has_no_effect(cx, a) && has_no_effect(cx, b),
ExprKind::Array(v) | ExprKind::Tup(v) => v.iter().all(|val| has_no_effect(cx, val)),
ExprKind::Repeat(inner, _)
Expand All @@ -233,8 +239,8 @@ fn has_no_effect(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
| ExprKind::Unary(_, inner)
| ExprKind::Field(inner, _)
| ExprKind::AddrOf(_, _, inner) => has_no_effect(cx, inner),
ExprKind::Struct(_, fields, ref base) => {
!has_drop(cx, cx.typeck_results().expr_ty(expr))
ExprKind::Struct(_, fields, base) => {
!expr_ty_has_significant_drop(cx, expr)
&& fields.iter().all(|field| has_no_effect(cx, field.expr))
&& match &base {
StructTailExpr::None | StructTailExpr::DefaultFields(_) => true,
Expand All @@ -252,7 +258,7 @@ fn has_no_effect(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
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))
!expr_ty_has_significant_drop(cx, expr) && args.iter().all(|arg| has_no_effect(cx, arg))
} else {
false
}
Expand Down
53 changes: 53 additions & 0 deletions tests/ui/no_effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,56 @@ fn main() {
Cout << 142;
-Cout;
}

fn issue14592() {
struct MyStruct {
_inner: MyInner,
}
struct MyInner {}

impl Drop for MyInner {
fn drop(&mut self) {
println!("dropping");
}
}

let x = MyStruct { _inner: MyInner {} };

let closure = || {
// Do not lint: dropping the assignment or assigning to `_` would
// change the output.
let _x = x;
};

println!("1");
closure();
println!("2");

struct Innocuous {
a: i32,
}

// Do not lint: one of the fields has a side effect.
let x = MyInner {};
let closure = || {
let _x = Innocuous {
a: {
x;
10
},
};
};

// Do not lint: the base has a side effect.
let x = MyInner {};
let closure = || {
let _x = Innocuous {
..Innocuous {
a: {
x;
10
},
}
};
};
}
2 changes: 1 addition & 1 deletion tests/ui/single_range_in_vec_init.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@aux-build:proc_macros.rs
//@no-rustfix: overlapping suggestions
#![allow(clippy::no_effect, clippy::useless_vec, unused)]
#![allow(clippy::no_effect, clippy::unnecessary_operation, clippy::useless_vec, unused)]
#![warn(clippy::single_range_in_vec_init)]
#![feature(generic_arg_infer)]

Expand Down
Loading