-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
Unnecessary references lint #138230
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
Open
obeis
wants to merge
2
commits into
rust-lang:main
Choose a base branch
from
obeis:lint-unnecessary-reference
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Unnecessary references lint #138230
Changes from all commits
Commits
Show all changes
2 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
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,74 @@ | ||
| use rustc_ast::BorrowKind; | ||
| use rustc_hir::{Expr, ExprKind, TyKind}; | ||
| use rustc_session::{declare_lint, declare_lint_pass}; | ||
|
|
||
| use crate::lints::{UnnecessaryRef, UnnecessaryRefSuggestion}; | ||
| use crate::{LateContext, LateLintPass, LintContext}; | ||
|
|
||
| declare_lint! { | ||
| /// The `unnecessary_refs` lint checks for unnecessary references. | ||
| /// | ||
| /// ### Example | ||
| /// | ||
| /// ```rust | ||
| /// #![warn(unnecessary_refs)] | ||
| /// | ||
| /// fn via_ref(x: *const (i32, i32)) -> *const i32 { | ||
| /// unsafe { &(*x).0 as *const i32 } | ||
| /// } | ||
| /// | ||
| /// fn main() { | ||
| /// let x = (0, 1); | ||
| /// let _r = via_ref(&x); | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| /// {{produces}} | ||
| /// | ||
| /// ### Explanation | ||
| /// | ||
| /// Creating unnecessary references is discouraged because it makes code | ||
| /// less explicit and can lead to undefined behavior. Creating a reference | ||
| /// induces aliasing assumptions that the compiler relies on, so an | ||
| /// otherwise-pointless reference can cause undefined behavior; it is also | ||
| /// UB if the reference is unaligned. Avoiding them keeps the code more | ||
| /// explicit and easier to reason about. | ||
| /// | ||
| /// This lint is "allow" by default because promoting it would require a | ||
| /// large amount of churn across the standard library to remove the many | ||
| /// unnecessary references it currently creates. | ||
| pub UNNECESSARY_REFS, | ||
| // FIXME: This should eventually be `Warn`, see the "Explanation" above. | ||
| Allow, | ||
| "creating unnecessary reference is discouraged" | ||
| } | ||
|
|
||
| declare_lint_pass!(UnnecessaryRefs => [UNNECESSARY_REFS]); | ||
|
|
||
| impl<'tcx> LateLintPass<'tcx> for UnnecessaryRefs { | ||
| fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) { | ||
| if let ExprKind::Cast(exp, ty) = expr.kind | ||
| && let ExprKind::AddrOf(BorrowKind::Ref, mutbl, addr_of_exp) = exp.kind | ||
| && let TyKind::Ptr(_) = ty.kind | ||
| && addr_of_exp.is_syntactic_place_expr() | ||
| { | ||
| let suggestion = if let Some(addr_of_span) = | ||
| addr_of_exp.span.find_ancestor_in_same_ctxt(expr.span) | ||
| && let Some(ty_span) = ty.span.find_ancestor_in_same_ctxt(expr.span) | ||
| && expr.span.can_be_used_for_suggestions() | ||
| && addr_of_span.can_be_used_for_suggestions() | ||
| && ty_span.can_be_used_for_suggestions() | ||
| { | ||
| UnnecessaryRefSuggestion::Spanful { | ||
| left: expr.span.until(addr_of_span), | ||
| right: addr_of_span.shrink_to_hi().until(ty_span.shrink_to_hi()), | ||
| mutbl: mutbl.ptr_str(), | ||
| } | ||
| } else { | ||
| UnnecessaryRefSuggestion::Spanless { mutbl: mutbl.ptr_str() } | ||
| }; | ||
|
|
||
| cx.emit_span_lint(UNNECESSARY_REFS, expr.span, UnnecessaryRef { suggestion }); | ||
| } | ||
| } | ||
| } | ||
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,87 @@ | ||
| //@ check-pass | ||
| //@ run-rustfix | ||
| //@ rustfix-only-machine-applicable | ||
|
|
||
| #![allow(dead_code, unused_variables)] | ||
| #![warn(unnecessary_refs)] | ||
|
|
||
| struct A { | ||
| a: i32, | ||
| b: u8, | ||
| } | ||
|
|
||
| fn via_ref(x: *const (i32, i32)) -> *const i32 { | ||
| unsafe { &raw const (*x).0 } | ||
| //~^ WARN creating an intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs] | ||
| } | ||
|
|
||
| fn via_ref_struct(x: *const A) -> *const u8 { | ||
| unsafe { &raw const (*x).b } | ||
| //~^ WARN creating an intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs] | ||
| } | ||
|
|
||
| fn via_ref_mut(x: *mut (i32, i32)) -> *mut i32 { | ||
| unsafe { &raw mut (*x).0 } | ||
| //~^ WARN creating an intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs] | ||
| } | ||
|
|
||
| fn via_ref_struct_mut(x: *mut A) -> *mut i32 { | ||
| unsafe { &raw mut (*x).a } | ||
| //~^ WARN creating an intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs] | ||
| } | ||
|
|
||
| fn multiple_casts(x: *const (i32, i32)) -> *const u8 { | ||
| unsafe { &raw const (*x).0 as *const u8 } | ||
| //~^ WARN creating an intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs] | ||
| } | ||
|
|
||
| fn ret_i32() -> i32 { | ||
| 0 | ||
| } | ||
|
|
||
| fn temporaries() { | ||
| let _ = &1 as *const i32; | ||
| let _ = &(1 + 2) as *const i32; | ||
| let _ = &ret_i32() as *const i32; | ||
| let _ = &(1, 2) as *const (i32, i32); | ||
| let _ = &[1, 2, 3] as *const [i32; 3]; | ||
| let _ = &A { a: 0, b: 0 } as *const A; | ||
| let _ = &mut 4 as *mut i32; | ||
| } | ||
|
|
||
| fn inner_blocks() { | ||
| let x = 0; | ||
| let _ = { &raw const x }; | ||
| //~^ WARN creating an intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs] | ||
|
|
||
| let _ = { | ||
| let y = 0; | ||
| { &raw const y } | ||
| //~^ WARN creating an intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs] | ||
| }; | ||
|
|
||
| let _ = { &1 as *const i32 }; | ||
| } | ||
|
|
||
| macro_rules! ref_cast { | ||
| ($e:expr) => { | ||
| &$e as *const i32 | ||
| //~^ WARN creating an intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs] | ||
| }; | ||
| } | ||
|
|
||
| fn from_macro(x: *const i32) -> *const i32 { | ||
| unsafe { ref_cast!(*x) } | ||
| } | ||
|
|
||
| fn main() { | ||
| let a = 0; | ||
| let a = &raw const a; | ||
| //~^ WARN creating an intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs] | ||
|
|
||
| let mut b = 0; | ||
| let b = &raw mut b; | ||
| //~^ WARN creating an intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs] | ||
|
|
||
| let i = &1 as *const i32; | ||
| } |
|
Urgau marked this conversation as resolved.
|
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,87 @@ | ||
| //@ check-pass | ||
| //@ run-rustfix | ||
| //@ rustfix-only-machine-applicable | ||
|
|
||
| #![allow(dead_code, unused_variables)] | ||
| #![warn(unnecessary_refs)] | ||
|
|
||
| struct A { | ||
| a: i32, | ||
| b: u8, | ||
| } | ||
|
|
||
| fn via_ref(x: *const (i32, i32)) -> *const i32 { | ||
| unsafe { &(*x).0 as *const i32 } | ||
| //~^ WARN creating an intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs] | ||
| } | ||
|
|
||
| fn via_ref_struct(x: *const A) -> *const u8 { | ||
| unsafe { &(*x).b as *const u8 } | ||
| //~^ WARN creating an intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs] | ||
| } | ||
|
|
||
| fn via_ref_mut(x: *mut (i32, i32)) -> *mut i32 { | ||
| unsafe { &mut (*x).0 as *mut i32 } | ||
| //~^ WARN creating an intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs] | ||
| } | ||
|
|
||
| fn via_ref_struct_mut(x: *mut A) -> *mut i32 { | ||
| unsafe { &mut (*x).a as *mut i32 } | ||
| //~^ WARN creating an intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs] | ||
| } | ||
|
|
||
| fn multiple_casts(x: *const (i32, i32)) -> *const u8 { | ||
| unsafe { &(*x).0 as *const i32 as *const u8 } | ||
| //~^ WARN creating an intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs] | ||
| } | ||
|
|
||
| fn ret_i32() -> i32 { | ||
| 0 | ||
| } | ||
|
|
||
| fn temporaries() { | ||
| let _ = &1 as *const i32; | ||
| let _ = &(1 + 2) as *const i32; | ||
| let _ = &ret_i32() as *const i32; | ||
| let _ = &(1, 2) as *const (i32, i32); | ||
| let _ = &[1, 2, 3] as *const [i32; 3]; | ||
| let _ = &A { a: 0, b: 0 } as *const A; | ||
| let _ = &mut 4 as *mut i32; | ||
| } | ||
|
|
||
| fn inner_blocks() { | ||
| let x = 0; | ||
| let _ = { &x as *const i32 }; | ||
| //~^ WARN creating an intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs] | ||
|
|
||
| let _ = { | ||
| let y = 0; | ||
| { &y as *const i32 } | ||
| //~^ WARN creating an intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs] | ||
| }; | ||
|
|
||
| let _ = { &1 as *const i32 }; | ||
| } | ||
|
|
||
| macro_rules! ref_cast { | ||
| ($e:expr) => { | ||
| &$e as *const i32 | ||
| //~^ WARN creating an intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs] | ||
| }; | ||
| } | ||
|
|
||
| fn from_macro(x: *const i32) -> *const i32 { | ||
| unsafe { ref_cast!(*x) } | ||
| } | ||
|
|
||
| fn main() { | ||
| let a = 0; | ||
| let a = &a as *const i32; | ||
| //~^ WARN creating an intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs] | ||
|
|
||
| let mut b = 0; | ||
| let b = &mut b as *mut i32; | ||
| //~^ WARN creating an intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs] | ||
|
|
||
| let i = &1 as *const i32; | ||
| } |
Oops, something went wrong.
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.