-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
uplift clippy::clone_double_ref
to rustc
#109842
Closed
+251
−209
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f9dde4b
uplift `clippy::clone_double_ref` to rustc
fee1-dead f89f190
clippy changes to uplift and remove lint
fee1-dead 744b241
fix it in librustdoc
fee1-dead 63c5d38
temp
fee1-dead 8a919eb
fix clippy
fee1-dead 0dbc32d
merge clone_double_ref with noop_method_call
fee1-dead 82fbdaa
rename to suspicious_double_ref_op and fix CI
fee1-dead dc7f055
add example, adjust
fee1-dead File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
use crate::context::LintContext; | ||
use crate::lints::NoopMethodCallDiag; | ||
use crate::lints::{CloneDoubleRef, NoopMethodCallDiag}; | ||
use crate::LateContext; | ||
use crate::LateLintPass; | ||
use rustc_hir::def::DefKind; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_middle::ty; | ||
use rustc_middle::ty::adjustment::Adjust; | ||
use rustc_span::symbol::sym; | ||
|
||
declare_lint! { | ||
|
@@ -15,7 +16,6 @@ declare_lint! { | |
/// | ||
/// ```rust | ||
/// # #![allow(unused)] | ||
/// #![warn(noop_method_call)] | ||
/// struct Foo; | ||
/// let foo = &Foo; | ||
/// let clone: &Foo = foo.clone(); | ||
|
@@ -31,18 +31,48 @@ declare_lint! { | |
/// calling `clone` on a `&T` where `T` does not implement clone, actually doesn't do anything | ||
/// as references are copy. This lint detects these calls and warns the user about them. | ||
pub NOOP_METHOD_CALL, | ||
Allow, | ||
Warn, | ||
"detects the use of well-known noop methods" | ||
} | ||
|
||
declare_lint_pass!(NoopMethodCall => [NOOP_METHOD_CALL]); | ||
declare_lint! { | ||
/// The `suspicious_double_ref_op` lint checks for usage of `.clone()`/`.borrow()`/`.deref()` | ||
/// on an `&&T` when `T: !Deref/Borrow/Clone`, which means the call will copy the inner `&T`, | ||
/// instead of performing the operation on the underlying `T` and can be confusing. | ||
/// | ||
/// ### Example | ||
/// | ||
/// ```rust | ||
/// # #![allow(unused)] | ||
/// struct Foo; | ||
/// let foo = &&Foo; | ||
/// let clone: &Foo = foo.clone(); | ||
/// ``` | ||
/// | ||
/// {{produces}} | ||
/// | ||
/// ### Explanation | ||
/// | ||
/// Since `Foo` doesn't implement `Clone`, running `.clone()` only dereferences the double | ||
/// reference, instead of cloning the inner type which should be what was intended. | ||
pub SUSPICIOUS_DOUBLE_REF_OP, | ||
Warn, | ||
"using `clone` on `&&T`" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This lint isn't |
||
} | ||
|
||
declare_lint_pass!(NoopMethodCall => [NOOP_METHOD_CALL, SUSPICIOUS_DOUBLE_REF_OP]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for NoopMethodCall { | ||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { | ||
// We only care about method calls. | ||
let ExprKind::MethodCall(call, receiver, ..) = &expr.kind else { | ||
return | ||
let ExprKind::MethodCall(call, receiver, _, call_span) = &expr.kind else { | ||
return; | ||
}; | ||
|
||
if call_span.from_expansion() { | ||
return; | ||
} | ||
|
||
// We only care about method calls corresponding to the `Clone`, `Deref` and `Borrow` | ||
// traits and ignore any other method call. | ||
let did = match cx.typeck_results().type_dependent_def(expr.hir_id) { | ||
|
@@ -70,25 +100,40 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall { | |
}; | ||
// (Re)check that it implements the noop diagnostic. | ||
let Some(name) = cx.tcx.get_diagnostic_name(i.def_id()) else { return }; | ||
if !matches!( | ||
name, | ||
sym::noop_method_borrow | sym::noop_method_clone | sym::noop_method_deref | ||
) { | ||
return; | ||
} | ||
|
||
let op = match name { | ||
sym::noop_method_borrow => "borrowing", | ||
sym::noop_method_clone => "cloning", | ||
sym::noop_method_deref => "dereferencing", | ||
_ => return, | ||
}; | ||
|
||
let receiver_ty = cx.typeck_results().expr_ty(receiver); | ||
let expr_ty = cx.typeck_results().expr_ty_adjusted(expr); | ||
if receiver_ty != expr_ty { | ||
// This lint will only trigger if the receiver type and resulting expression \ | ||
// type are the same, implying that the method call is unnecessary. | ||
|
||
let arg_adjustments = cx.typeck_results().expr_adjustments(receiver); | ||
|
||
// If there is any user defined auto-deref step, then we don't want to warn. | ||
// https://github.com/rust-lang/rust-clippy/issues/9272 | ||
if arg_adjustments.iter().any(|adj| matches!(adj.kind, Adjust::Deref(Some(_)))) { | ||
return; | ||
} | ||
|
||
let expr_span = expr.span; | ||
let span = expr_span.with_lo(receiver.span.hi()); | ||
cx.emit_spanned_lint( | ||
NOOP_METHOD_CALL, | ||
span, | ||
NoopMethodCallDiag { method: call.ident.name, receiver_ty, label: span }, | ||
); | ||
|
||
if receiver_ty == expr_ty { | ||
cx.emit_spanned_lint( | ||
NOOP_METHOD_CALL, | ||
span, | ||
NoopMethodCallDiag { method: call.ident.name, receiver_ty, label: span }, | ||
); | ||
} else { | ||
cx.emit_spanned_lint( | ||
SUSPICIOUS_DOUBLE_REF_OP, | ||
span, | ||
CloneDoubleRef { call: call.ident.name, ty: expr_ty, op }, | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like this change and the associated changes to lib/compiler should be done in a separate PR.
Or at least the PR title and description need to be updated to reflect that this is also bumping NOOP_METHOD_CALL to warn.
But I would prefer the first option. Just uplifting
clone_double_ref
seems to me very easy to r+, having to approve both is a bit harder to disentangle (esp in case we need to unland one in the future, e.g. due to lots of false positives in some yet undiscovered case -- which I have no reason to believe may happen, but stranger things have happened before).