Skip to content

Fix FP of identity_op when encountering Default::default() #15028

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

Merged
merged 3 commits into from
Jun 15, 2025
Merged
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
70 changes: 65 additions & 5 deletions clippy_lints/src/operators/identity_op.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use clippy_utils::consts::{ConstEvalCtxt, Constant, FullInt};
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::{clip, peel_hir_expr_refs, unsext};
use clippy_utils::{ExprUseNode, clip, expr_use_ctxt, peel_hir_expr_refs, unsext};
use rustc_errors::Applicability;
use rustc_hir::{BinOpKind, Expr, ExprKind, Node};
use rustc_hir::def::{DefKind, Res};
use rustc_hir::{BinOpKind, Expr, ExprKind, Node, Path, QPath};
use rustc_lint::LateContext;
use rustc_middle::ty;
use rustc_span::Span;
use rustc_span::{Span, kw};

use super::IDENTITY_OP;

Expand All @@ -17,7 +18,7 @@ pub(crate) fn check<'tcx>(
left: &'tcx Expr<'_>,
right: &'tcx Expr<'_>,
) {
if !is_allowed(cx, op, left, right) {
if !is_allowed(cx, expr, op, left, right) {
return;
}

Expand Down Expand Up @@ -165,7 +166,22 @@ fn needs_parenthesis(cx: &LateContext<'_>, binary: &Expr<'_>, child: &Expr<'_>)
Parens::Needed
}

fn is_allowed(cx: &LateContext<'_>, cmp: BinOpKind, left: &Expr<'_>, right: &Expr<'_>) -> bool {
fn is_allowed<'tcx>(
cx: &LateContext<'tcx>,
expr: &'tcx Expr<'tcx>,
cmp: BinOpKind,
left: &Expr<'tcx>,
right: &Expr<'tcx>,
) -> bool {
// Exclude case where the left or right side is associated function call returns a type which is
// `Self` that is not given explicitly, and the expression is not a let binding's init
// expression and the let binding has a type annotation, or a function's return value.
if (is_assoc_fn_without_type_instance(cx, left) || is_assoc_fn_without_type_instance(cx, right))
&& !is_expr_used_with_type_annotation(cx, expr)
{
return false;
}

// This lint applies to integers and their references
cx.typeck_results().expr_ty(left).peel_refs().is_integral()
&& cx.typeck_results().expr_ty(right).peel_refs().is_integral()
Expand Down Expand Up @@ -234,3 +250,47 @@ fn span_ineffective_operation(
applicability,
);
}

fn is_expr_used_with_type_annotation<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
match expr_use_ctxt(cx, expr).use_node(cx) {
ExprUseNode::LetStmt(letstmt) => letstmt.ty.is_some(),
ExprUseNode::Return(_) => true,
_ => false,
}
}

/// Check if the expression is an associated function without a type instance.
/// Example:
/// ```
/// trait Def {
/// fn def() -> Self;
/// }
/// impl Def for usize {
/// fn def() -> Self {
/// 0
/// }
/// }
/// fn test() {
/// let _ = 0usize + &Default::default();
/// let _ = 0usize + &Def::def();
/// }
/// ```
fn is_assoc_fn_without_type_instance<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) -> bool {
if let ExprKind::Call(func, _) = peel_hir_expr_refs(expr).0.kind
&& let ExprKind::Path(QPath::Resolved(
// If it's not None, don't need to go further.
None,
Path {
res: Res::Def(DefKind::AssocFn, def_id),
..
},
)) = func.kind
&& let output_ty = cx.tcx.fn_sig(def_id).instantiate_identity().skip_binder().output()
&& let ty::Param(ty::ParamTy {
name: kw::SelfUpper, ..
}) = output_ty.kind()
{
return true;
}
false
}
46 changes: 46 additions & 0 deletions tests/ui/identity_op.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,49 @@ fn issue_13470() {
let _: u64 = 1u64 + ((x as i32 + y as i32) as u64);
//~^ identity_op
}

fn issue_14932() {
let _ = 0usize + &Default::default(); // no error

0usize + &Default::default(); // no error

<usize as Default>::default();
//~^ identity_op

let _ = usize::default();
//~^ identity_op

let _n: usize = Default::default();
//~^ identity_op
}

// Expr's type can be inferred by the function's return type
fn issue_14932_2() -> usize {
Default::default()
//~^ identity_op
}

trait Def {
fn def() -> Self;
}

impl Def for usize {
fn def() -> Self {
0
}
}

fn issue_14932_3() {
let _ = 0usize + &Def::def(); // no error

0usize + &Def::def(); // no error

<usize as Def>::def();
//~^ identity_op

let _ = usize::def();
//~^ identity_op

let _n: usize = Def::def();
//~^ identity_op
}
46 changes: 46 additions & 0 deletions tests/ui/identity_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,49 @@ fn issue_13470() {
let _: u64 = 1u64 + ((x as i32 + y as i32) as u64 + 0u64);
//~^ identity_op
}

fn issue_14932() {
let _ = 0usize + &Default::default(); // no error

0usize + &Default::default(); // no error

0usize + &<usize as Default>::default();
//~^ identity_op

let _ = 0usize + &usize::default();
//~^ identity_op

let _n: usize = 0usize + &Default::default();
//~^ identity_op
}

// Expr's type can be inferred by the function's return type
fn issue_14932_2() -> usize {
0usize + &Default::default()
//~^ identity_op
}

trait Def {
fn def() -> Self;
}

impl Def for usize {
fn def() -> Self {
0
}
}

fn issue_14932_3() {
let _ = 0usize + &Def::def(); // no error

0usize + &Def::def(); // no error

0usize + &<usize as Def>::def();
//~^ identity_op

let _ = 0usize + &usize::def();
//~^ identity_op

let _n: usize = 0usize + &Def::def();
//~^ identity_op
}
44 changes: 43 additions & 1 deletion tests/ui/identity_op.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -379,5 +379,47 @@ error: this operation has no effect
LL | let _: u64 = 1u64 + ((x as i32 + y as i32) as u64 + 0u64);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider reducing it to: `((x as i32 + y as i32) as u64)`

error: aborting due to 63 previous errors
error: this operation has no effect
--> tests/ui/identity_op.rs:321:5
|
LL | 0usize + &<usize as Default>::default();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider reducing it to: `<usize as Default>::default()`

error: this operation has no effect
--> tests/ui/identity_op.rs:324:13
|
LL | let _ = 0usize + &usize::default();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider reducing it to: `usize::default()`

error: this operation has no effect
--> tests/ui/identity_op.rs:327:21
|
LL | let _n: usize = 0usize + &Default::default();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider reducing it to: `Default::default()`

error: this operation has no effect
--> tests/ui/identity_op.rs:333:5
|
LL | 0usize + &Default::default()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider reducing it to: `Default::default()`

error: this operation has no effect
--> tests/ui/identity_op.rs:352:5
|
LL | 0usize + &<usize as Def>::def();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider reducing it to: `<usize as Def>::def()`

error: this operation has no effect
--> tests/ui/identity_op.rs:355:13
|
LL | let _ = 0usize + &usize::def();
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider reducing it to: `usize::def()`

error: this operation has no effect
--> tests/ui/identity_op.rs:358:21
|
LL | let _n: usize = 0usize + &Def::def();
| ^^^^^^^^^^^^^^^^^^^^ help: consider reducing it to: `Def::def()`

error: aborting due to 70 previous errors