Skip to content

Commit 2e3c031

Browse files
committed
Rename has_nontrivial_oprand to is_operator_overrided
Simpfy code of `is_operator_overrided`, directly use `is_method_call` to check if operator is overrided, at least one oprand of binary-expr must be ADT-type So no need to check type of lhs and rhs
1 parent 8977423 commit 2e3c031

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

clippy_lints/src/no_effect.rs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use rustc_hir::{
1010
use rustc_infer::infer::TyCtxtInferExt as _;
1111
use rustc_lint::{LateContext, LateLintPass, LintContext};
1212
use rustc_middle::lint::in_external_macro;
13-
use rustc_middle::ty;
1413
use rustc_session::declare_lint_pass;
1514
use std::ops::Deref;
1615

@@ -94,7 +93,7 @@ fn check_no_effect(cx: &LateContext<'_>, stmt: &Stmt<'_>) -> bool {
9493
}
9594
let expr = peel_blocks(expr);
9695
// assume nontrivial oprand of `Binary` Expr can skip `check_unnecessary_operation`
97-
if has_nontrivial_oprand(cx, expr) {
96+
if is_operator_overrided(cx, expr) {
9897
return true;
9998
}
10099
if has_no_effect(cx, expr) {
@@ -163,31 +162,19 @@ fn check_no_effect(cx: &LateContext<'_>, stmt: &Stmt<'_>) -> bool {
163162
false
164163
}
165164

166-
fn has_nontrivial_oprand(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
165+
fn is_operator_overrided(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
167166
// It's very hard or impossable to check whether overrided operator have side-effect this lint.
168167
// So, this function assume user-defined binary operator is overrided with an side-effect.
169-
// The definition of user-defined structure here is `struct`, `enum`, `uniom`,
168+
// The definition of user-defined structure here is ADT-type,
170169
// Althrough this will weaken the ability of this lint, less error lint-fix happen.
171170
match expr.kind {
172-
ExprKind::Binary(_, lhs, rhs) => {
173-
// get type of lhs and rhs
174-
let tyck_result = cx.typeck_results();
175-
let ty_lhs = tyck_result.expr_ty(lhs).kind();
176-
let ty_rhs = tyck_result.expr_ty(rhs).kind();
177-
// check whether lhs is a user-defined structure
178-
// only need to check lhs in fact
179-
let ud_lhs = match ty_lhs {
180-
ty::Adt(adt_def, _) => adt_def.is_struct() || adt_def.is_enum() || adt_def.is_union(),
181-
_ => false,
182-
};
183-
let ud_rhs = match ty_rhs {
184-
ty::Adt(adt_def, _) => adt_def.is_struct() || adt_def.is_enum() || adt_def.is_union(),
185-
_ => false,
186-
};
171+
ExprKind::Binary(..) => {
172+
// No need to check type of `lhs` and `rhs`
173+
// because if the operator is overrided, at least one operand is ADT type
187174

188175
// reference: rust/compiler/rustc_middle/src/ty/typeck_results.rs: `is_method_call`.
189176
// use this function to check whether operator is overrided in `ExprKind::Binary`.
190-
(ud_lhs || ud_rhs) && tyck_result.is_method_call(expr)
177+
cx.typeck_results().is_method_call(expr)
191178
},
192179
_ => false,
193180
}

0 commit comments

Comments
 (0)