Add check for unary-operator

Fix typo and add test for unary-opeator
This commit is contained in:
cocodery 2023-12-06 12:17:48 +08:00
parent 2e3c031528
commit ee2354badf
3 changed files with 44 additions and 36 deletions

View file

@ -93,7 +93,7 @@ fn check_no_effect(cx: &LateContext<'_>, stmt: &Stmt<'_>) -> bool {
}
let expr = peel_blocks(expr);
// assume nontrivial oprand of `Binary` Expr can skip `check_unnecessary_operation`
if is_operator_overrided(cx, expr) {
if is_operator_overriden(cx, expr) {
return true;
}
if has_no_effect(cx, expr) {
@ -162,18 +162,18 @@ fn check_no_effect(cx: &LateContext<'_>, stmt: &Stmt<'_>) -> bool {
false
}
fn is_operator_overrided(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
fn is_operator_overriden(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
// It's very hard or impossable to check whether overrided operator have side-effect this lint.
// So, this function assume user-defined binary operator is overrided with an side-effect.
// So, this function assume user-defined operator is overrided with an side-effect.
// The definition of user-defined structure here is ADT-type,
// Althrough this will weaken the ability of this lint, less error lint-fix happen.
match expr.kind {
ExprKind::Binary(..) => {
ExprKind::Binary(..) | ExprKind::Unary(..) => {
// No need to check type of `lhs` and `rhs`
// because if the operator is overrided, at least one operand is ADT type
// reference: rust/compiler/rustc_middle/src/ty/typeck_results.rs: `is_method_call`.
// use this function to check whether operator is overrided in `ExprKind::Binary`.
// use this function to check whether operator is overrided in `ExprKind::{Binary, Unary}`.
cx.typeck_results().is_method_call(expr)
},
_ => false,