Skip to content

Reenable linting on UFCS deref calls #14808

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 1 commit into from
May 16, 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
67 changes: 28 additions & 39 deletions clippy_lints/src/dereference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt, TypeckResults};
use rustc_session::impl_lint_pass;
use rustc_span::symbol::sym;
use rustc_span::{Span, Symbol};
use std::borrow::Cow;

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -252,13 +253,14 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> {
}

let typeck = cx.typeck_results();
let Some((kind, sub_expr)) = try_parse_ref_op(cx.tcx, typeck, expr) else {
let Some((kind, sub_expr, skip_expr)) = try_parse_ref_op(cx.tcx, typeck, expr) else {
// The whole chain of reference operations has been seen
if let Some((state, data)) = self.state.take() {
report(cx, expr, state, data, typeck);
}
return;
};
self.skip_expr = skip_expr;

match (self.state.take(), kind) {
(None, kind) => {
Expand Down Expand Up @@ -671,42 +673,38 @@ fn try_parse_ref_op<'tcx>(
tcx: TyCtxt<'tcx>,
typeck: &'tcx TypeckResults<'_>,
expr: &'tcx Expr<'_>,
) -> Option<(RefOp, &'tcx Expr<'tcx>)> {
let (is_ufcs, def_id, arg) = match expr.kind {
ExprKind::MethodCall(_, arg, [], _) => (false, typeck.type_dependent_def_id(expr.hir_id)?, arg),
) -> Option<(RefOp, &'tcx Expr<'tcx>, Option<HirId>)> {
let (call_path_id, def_id, arg) = match expr.kind {
ExprKind::MethodCall(_, arg, [], _) => (None, typeck.type_dependent_def_id(expr.hir_id)?, arg),
ExprKind::Call(
Expr {
kind: ExprKind::Path(path),
&Expr {
kind: ExprKind::Path(QPath::Resolved(None, path)),
hir_id,
..
},
[arg],
) => (true, typeck.qpath_res(path, *hir_id).opt_def_id()?, arg),
) => (Some(hir_id), path.res.opt_def_id()?, arg),
ExprKind::Unary(UnOp::Deref, sub_expr) if !typeck.expr_ty(sub_expr).is_raw_ptr() => {
return Some((RefOp::Deref, sub_expr));
return Some((RefOp::Deref, sub_expr, None));
},
ExprKind::AddrOf(BorrowKind::Ref, mutability, sub_expr) => {
return Some((RefOp::AddrOf(mutability), sub_expr, None));
},
ExprKind::AddrOf(BorrowKind::Ref, mutability, sub_expr) => return Some((RefOp::AddrOf(mutability), sub_expr)),
_ => return None,
};
if tcx.is_diagnostic_item(sym::deref_method, def_id) {
Some((
RefOp::Method {
mutbl: Mutability::Not,
is_ufcs,
},
arg,
))
} else if tcx.trait_of_item(def_id)? == tcx.lang_items().deref_mut_trait()? {
Some((
RefOp::Method {
mutbl: Mutability::Mut,
is_ufcs,
},
arg,
))
} else {
None
}
let mutbl = match tcx.get_diagnostic_name(def_id) {
Some(sym::deref_method) => Mutability::Not,
Some(sym::deref_mut_method) => Mutability::Mut,
_ => return None,
};
Some((
RefOp::Method {
mutbl,
is_ufcs: call_path_id.is_some(),
},
arg,
call_path_id,
))
}

// Checks if the adjustments contains a deref of `ManuallyDrop<_>`
Expand Down Expand Up @@ -944,7 +942,7 @@ fn report<'tcx>(
mutbl,
} => {
let mut app = Applicability::MachineApplicable;
let (expr_str, _expr_is_macro_call) =
let (expr_str, expr_is_macro_call) =
snippet_with_context(cx, expr.span, data.first_expr.span.ctxt(), "..", &mut app);
let ty = typeck.expr_ty(expr);
let (_, ref_count) = peel_middle_ty_refs(ty);
Expand All @@ -968,20 +966,11 @@ fn report<'tcx>(
"&"
};

// expr_str (the suggestion) is never shown if is_final_ufcs is true, since it's
// `expr.kind == ExprKind::Call`. Therefore, this is, afaik, always unnecessary.
/*
expr_str = if !expr_is_macro_call && is_final_ufcs && expr.precedence() < ExprPrecedence::Prefix {
let expr_str = if !expr_is_macro_call && is_ufcs && expr.precedence() < ExprPrecedence::Prefix {
Cow::Owned(format!("({expr_str})"))
} else {
expr_str
};
*/

// Fix #10850, do not lint if it's `Foo::deref` instead of `foo.deref()`.
if is_ufcs {
return;
}

span_lint_and_sugg(
cx,
Expand Down
11 changes: 7 additions & 4 deletions tests/ui/explicit_deref_methods.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
clippy::needless_borrow,
clippy::no_effect,
clippy::uninlined_format_args,
clippy::unnecessary_literal_unwrap
clippy::unnecessary_literal_unwrap,
clippy::deref_addrof
)]

use std::ops::{Deref, DerefMut};
Expand Down Expand Up @@ -87,9 +88,6 @@ fn main() {
let b = &*opt_a.unwrap();
//~^ explicit_deref_methods

// make sure `Aaa::deref` instead of `aaa.deref()` is not linted, as well as fully qualified
// syntax

Aaa::deref(&Aaa);
Aaa::deref_mut(&mut Aaa);
<Aaa as Deref>::deref(&Aaa);
Expand Down Expand Up @@ -139,4 +137,9 @@ fn main() {
let no_lint = NoLint(42);
let b = no_lint.deref();
let b = no_lint.deref_mut();

let _ = &*&"foo"; //~ explicit_deref_methods
let mut x = String::new();
let _ = &&mut **&mut x; //~ explicit_deref_methods
let _ = &&mut ***(&mut &mut x); //~ explicit_deref_methods
}
11 changes: 7 additions & 4 deletions tests/ui/explicit_deref_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
clippy::needless_borrow,
clippy::no_effect,
clippy::uninlined_format_args,
clippy::unnecessary_literal_unwrap
clippy::unnecessary_literal_unwrap,
clippy::deref_addrof
)]

use std::ops::{Deref, DerefMut};
Expand Down Expand Up @@ -87,9 +88,6 @@ fn main() {
let b = opt_a.unwrap().deref();
//~^ explicit_deref_methods

// make sure `Aaa::deref` instead of `aaa.deref()` is not linted, as well as fully qualified
// syntax

Aaa::deref(&Aaa);
Aaa::deref_mut(&mut Aaa);
<Aaa as Deref>::deref(&Aaa);
Expand Down Expand Up @@ -139,4 +137,9 @@ fn main() {
let no_lint = NoLint(42);
let b = no_lint.deref();
let b = no_lint.deref_mut();

let _ = &Deref::deref(&"foo"); //~ explicit_deref_methods
let mut x = String::new();
let _ = &DerefMut::deref_mut(&mut x); //~ explicit_deref_methods
let _ = &DerefMut::deref_mut((&mut &mut x).deref_mut()); //~ explicit_deref_methods
}
44 changes: 31 additions & 13 deletions tests/ui/explicit_deref_methods.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: explicit `deref` method call
--> tests/ui/explicit_deref_methods.rs:54:19
--> tests/ui/explicit_deref_methods.rs:55:19
|
LL | let b: &str = a.deref();
| ^^^^^^^^^ help: try: `&*a`
Expand All @@ -8,70 +8,88 @@ LL | let b: &str = a.deref();
= help: to override `-D warnings` add `#[allow(clippy::explicit_deref_methods)]`

error: explicit `deref_mut` method call
--> tests/ui/explicit_deref_methods.rs:57:23
--> tests/ui/explicit_deref_methods.rs:58:23
|
LL | let b: &mut str = a.deref_mut();
| ^^^^^^^^^^^^^ help: try: `&mut **a`

error: explicit `deref` method call
--> tests/ui/explicit_deref_methods.rs:61:39
--> tests/ui/explicit_deref_methods.rs:62:39
|
LL | let b: String = format!("{}, {}", a.deref(), a.deref());
| ^^^^^^^^^ help: try: `&*a`

error: explicit `deref` method call
--> tests/ui/explicit_deref_methods.rs:61:50
--> tests/ui/explicit_deref_methods.rs:62:50
|
LL | let b: String = format!("{}, {}", a.deref(), a.deref());
| ^^^^^^^^^ help: try: `&*a`

error: explicit `deref` method call
--> tests/ui/explicit_deref_methods.rs:65:20
--> tests/ui/explicit_deref_methods.rs:66:20
|
LL | println!("{}", a.deref());
| ^^^^^^^^^ help: try: `&*a`

error: explicit `deref` method call
--> tests/ui/explicit_deref_methods.rs:69:11
--> tests/ui/explicit_deref_methods.rs:70:11
|
LL | match a.deref() {
| ^^^^^^^^^ help: try: `&*a`

error: explicit `deref` method call
--> tests/ui/explicit_deref_methods.rs:74:28
--> tests/ui/explicit_deref_methods.rs:75:28
|
LL | let b: String = concat(a.deref());
| ^^^^^^^^^ help: try: `&*a`

error: explicit `deref` method call
--> tests/ui/explicit_deref_methods.rs:77:13
--> tests/ui/explicit_deref_methods.rs:78:13
|
LL | let b = just_return(a).deref();
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `just_return(a)`

error: explicit `deref` method call
--> tests/ui/explicit_deref_methods.rs:80:28
--> tests/ui/explicit_deref_methods.rs:81:28
|
LL | let b: String = concat(just_return(a).deref());
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `just_return(a)`

error: explicit `deref` method call
--> tests/ui/explicit_deref_methods.rs:83:19
--> tests/ui/explicit_deref_methods.rs:84:19
|
LL | let b: &str = a.deref().deref();
| ^^^^^^^^^^^^^^^^^ help: try: `&**a`

error: explicit `deref` method call
--> tests/ui/explicit_deref_methods.rs:87:13
--> tests/ui/explicit_deref_methods.rs:88:13
|
LL | let b = opt_a.unwrap().deref();
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*opt_a.unwrap()`

error: explicit `deref` method call
--> tests/ui/explicit_deref_methods.rs:125:31
--> tests/ui/explicit_deref_methods.rs:123:31
|
LL | let b: &str = expr_deref!(a.deref());
| ^^^^^^^^^ help: try: `&*a`

error: aborting due to 12 previous errors
error: explicit `deref` method call
--> tests/ui/explicit_deref_methods.rs:141:14
|
LL | let _ = &Deref::deref(&"foo");
| ^^^^^^^^^^^^^^^^^^^^ help: try: `*&"foo"`

error: explicit `deref_mut` method call
--> tests/ui/explicit_deref_methods.rs:143:14
|
LL | let _ = &DerefMut::deref_mut(&mut x);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&mut **&mut x`

error: explicit `deref_mut` method call
--> tests/ui/explicit_deref_methods.rs:144:14
|
LL | let _ = &DerefMut::deref_mut((&mut &mut x).deref_mut());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&mut ***(&mut &mut x)`

error: aborting due to 15 previous errors