-
Notifications
You must be signed in to change notification settings - Fork 1.7k
new lint: borrow_deref_ref
#7930
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,118 @@ | ||
use crate::reference::DEREF_ADDROF; | ||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use clippy_utils::source::snippet_opt; | ||
use clippy_utils::ty::implements_trait; | ||
use clippy_utils::{get_parent_expr, is_lint_allowed}; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{ExprKind, UnOp}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_middle::mir::Mutability; | ||
use rustc_middle::ty; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for `&*(&T)`. | ||
/// | ||
/// ### Why is this bad? | ||
/// Dereferencing and then borrowing a reference value has no effect in most cases. | ||
/// | ||
/// ### Known problems | ||
/// false negative on such code: | ||
/// ``` | ||
/// let x = &12; | ||
/// let addr_x = &x as *const _ as usize; | ||
/// let addr_y = &&*x as *const _ as usize; // assert ok now, and lint triggerd. | ||
/// // But if we fix it, assert will fail. | ||
/// assert_ne!(addr_x, addr_y); | ||
lengyijun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// ``` | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// let s = &String::new(); | ||
/// | ||
/// // Bad | ||
/// let a: &String = &* s; | ||
/// foo(&*s); | ||
/// | ||
/// // Good | ||
/// let a: &String = s; | ||
/// foo(&**s); | ||
lengyijun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// | ||
/// fn foo(_: &str){ } | ||
lengyijun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// ``` | ||
#[clippy::version = "1.59.0"] | ||
pub BORROW_DEREF_REF, | ||
lengyijun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
complexity, | ||
"deref on an immutable reference returns the same type as itself" | ||
} | ||
|
||
declare_lint_pass!(BorrowDerefRef => [BORROW_DEREF_REF]); | ||
|
||
impl LateLintPass<'_> for BorrowDerefRef { | ||
fn check_expr(&mut self, cx: &LateContext<'_>, e: &rustc_hir::Expr<'_>) { | ||
if_chain! { | ||
if !e.span.from_expansion(); | ||
if let ExprKind::AddrOf(_, Mutability::Not, addrof_target) = e.kind; | ||
if !addrof_target.span.from_expansion(); | ||
if let ExprKind::Unary(UnOp::Deref, deref_target) = addrof_target.kind; | ||
if !deref_target.span.from_expansion(); | ||
if !matches!(deref_target.kind, ExprKind::Unary(UnOp::Deref, ..) ); | ||
let ref_ty = cx.typeck_results().expr_ty(deref_target); | ||
if let ty::Ref(_, inner_ty, Mutability::Not) = ref_ty.kind(); | ||
then{ | ||
|
||
if let Some(parent_expr) = get_parent_expr(cx, e){ | ||
lengyijun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if matches!(parent_expr.kind, ExprKind::Unary(UnOp::Deref, ..)) && | ||
!is_lint_allowed(cx, DEREF_ADDROF, parent_expr.hir_id) { | ||
return; | ||
} | ||
|
||
// modification to `&mut &*x` is different from `&mut x` | ||
if matches!(deref_target.kind, ExprKind::Path(..) | ||
| ExprKind::Field(..) | ||
| ExprKind::Index(..) | ||
| ExprKind::Unary(UnOp::Deref, ..)) | ||
&& matches!(parent_expr.kind, ExprKind::AddrOf(_, Mutability::Mut, _)) { | ||
return; | ||
} | ||
} | ||
|
||
span_lint_and_then( | ||
cx, | ||
BORROW_DEREF_REF, | ||
e.span, | ||
"deref on an immutable reference", | ||
|diag| { | ||
diag.span_suggestion( | ||
e.span, | ||
"if you would like to reborrow, try removing `&*`", | ||
snippet_opt(cx, deref_target.span).unwrap(), | ||
lengyijun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Applicability::MachineApplicable | ||
); | ||
|
||
// has deref trait -> give 2 help | ||
// doesn't have deref trait -> give 1 help | ||
if let Some(deref_trait_id) = cx.tcx.lang_items().deref_trait(){ | ||
if !implements_trait(cx, *inner_ty, deref_trait_id, &[]) { | ||
return; | ||
lengyijun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
diag.span_suggestion( | ||
e.span, | ||
"if you would like to deref, try using `&**`", | ||
format!( | ||
"&**{}", | ||
&snippet_opt(cx, deref_target.span).unwrap(), | ||
), | ||
Applicability::MaybeIncorrect | ||
); | ||
|
||
} | ||
); | ||
|
||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.