@@ -10,7 +10,6 @@ use rustc_hir::{
10
10
use rustc_infer:: infer:: TyCtxtInferExt as _;
11
11
use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
12
12
use rustc_middle:: lint:: in_external_macro;
13
- use rustc_middle:: ty;
14
13
use rustc_session:: declare_lint_pass;
15
14
use std:: ops:: Deref ;
16
15
@@ -94,7 +93,7 @@ fn check_no_effect(cx: &LateContext<'_>, stmt: &Stmt<'_>) -> bool {
94
93
}
95
94
let expr = peel_blocks ( expr) ;
96
95
// 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) {
98
97
return true ;
99
98
}
100
99
if has_no_effect ( cx, expr) {
@@ -163,31 +162,19 @@ fn check_no_effect(cx: &LateContext<'_>, stmt: &Stmt<'_>) -> bool {
163
162
false
164
163
}
165
164
166
- fn has_nontrivial_oprand ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) -> bool {
165
+ fn is_operator_overrided ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) -> bool {
167
166
// It's very hard or impossable to check whether overrided operator have side-effect this lint.
168
167
// 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 ,
170
169
// Althrough this will weaken the ability of this lint, less error lint-fix happen.
171
170
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
187
174
188
175
// reference: rust/compiler/rustc_middle/src/ty/typeck_results.rs: `is_method_call`.
189
176
// 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)
191
178
} ,
192
179
_ => false ,
193
180
}
0 commit comments