-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Implement a lint for implicit autoref of raw pointer dereference #103735
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
561dfaf
Implement a lint for implicit autoref of raw pointer dereference
WaffleLapkin 493b5fd
Make some auto-refs in std explicit
WaffleLapkin e1c0b69
Apply suggestions from code review
WaffleLapkin 4a7791d
`implicit_unsafe_autorefs`: make the lint warn by default
WaffleLapkin af4ade4
`implicit_unsafe_autorefs`: lint field access too
WaffleLapkin 45a6283
`implicit_unsafe_autorefs`: more std fixes
WaffleLapkin 0efeae6
`implicit_unsafe_autorefs`: compiler fixes
WaffleLapkin 459d6ad
strip trailing whitespace
WaffleLapkin ef427e6
`implicit_unsafe_autorefs`: ui test fixes
WaffleLapkin e53055b
Apply suggestion from code review
WaffleLapkin 6839707
`implicit_unsafe_autorefs`: fix clippy
WaffleLapkin 7b280e2
`implicit_unsafe_autorefs`: fix doctests
WaffleLapkin dda0fef
`implicit_unsafe_autorefs`: fix std tests
WaffleLapkin 94ad6c8
`implicit_unsafe_autorefs`: fix rust-analyzer bridge
WaffleLapkin c380f3e
`implicit_unsafe_autorefs`: fix miri tests too
WaffleLapkin 274eba7
`implicit_unsafe_autorefs` support built-in index places
WaffleLapkin 356f201
`implicit_unsafe_autorefs`: support overloaded deref
WaffleLapkin 6b238e6
`implicit_unsafe_autorefs`: note the reason why reference is created
WaffleLapkin 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use crate::{LateContext, LateLintPass, LintContext}; | ||
|
||
use rustc_errors::Applicability; | ||
use rustc_hir::{self as hir, Expr, ExprKind, UnOp}; | ||
use rustc_middle::ty::adjustment::{Adjust, AutoBorrow}; | ||
|
||
declare_lint! { | ||
/// The `implicit_unsafe_autorefs` lint checks for implicitly taken references to dereferences of raw pointers. | ||
/// | ||
/// ### Example | ||
/// | ||
/// ```rust | ||
/// unsafe fn fun(ptr: *mut [u8]) -> *mut [u8] { | ||
/// addr_of_mut!((*ptr)[..16]) | ||
/// // ^^^^^^ this calls `IndexMut::index_mut(&mut ..., ..16)`, | ||
/// // implicitly creating a reference | ||
/// } | ||
/// ``` | ||
/// | ||
/// {{produces}} | ||
/// | ||
/// ### Explanation | ||
/// | ||
/// When working with raw pointers it's usually undesirable to create references, | ||
/// since they inflict a lot of safety requirement. Unfortunately, it's possible | ||
/// to take a reference to a dereferece of a raw pointer implitly, which inflicts | ||
/// the usual reference requirements without you even knowing that. | ||
/// | ||
/// If you are sure, you can soundly take a reference, then you can take it explicitly: | ||
/// ```rust | ||
/// unsafe fn fun(ptr: *mut [u8]) -> *mut [u8] { | ||
/// addr_of_mut!((&mut *ptr)[..16]) | ||
/// } | ||
/// ``` | ||
/// | ||
/// Otherwise try to find an alternative way to achive your goals that work only with | ||
/// raw pointers: | ||
/// ```rust | ||
/// #![feature(slice_ptr_get)] | ||
/// | ||
/// unsafe fn fun(ptr: *mut [u8]) -> *mut [u8] { | ||
/// ptr.get_unchecked_mut(..16) | ||
/// } | ||
/// ``` | ||
pub IMPLICIT_UNSAFE_AUTOREFS, | ||
Deny, | ||
"implicit reference to a dereference of a raw pointer" | ||
} | ||
|
||
declare_lint_pass!(ImplicitUnsafeAutorefs => [IMPLICIT_UNSAFE_AUTOREFS]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for ImplicitUnsafeAutorefs { | ||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { | ||
let typeck = cx.typeck_results(); | ||
let adjustments_table = typeck.adjustments(); | ||
|
||
if let Some(adjustments) = adjustments_table.get(expr.hir_id) | ||
&& let [adjustment] = &**adjustments | ||
// An auto-borrow | ||
&& let Adjust::Borrow(AutoBorrow::Ref(_, mutbl)) = adjustment.kind | ||
// ... of a deref | ||
&& let ExprKind::Unary(UnOp::Deref, dereferenced) = expr.kind | ||
// ... of a raw pointer | ||
&& typeck.expr_ty(dereferenced).is_unsafe_ptr() | ||
WaffleLapkin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
let mutbl = hir::Mutability::prefix_str(&mutbl.into()); | ||
|
||
let msg = "implicit auto-ref creates a reference to a dereference of a raw pointer"; | ||
cx.struct_span_lint(IMPLICIT_UNSAFE_AUTOREFS, expr.span, msg, |lint| { | ||
lint | ||
.note("creating a reference inflicts a lot of safety requirements") | ||
WaffleLapkin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.multipart_suggestion( | ||
"if this reference is intentional, make it explicit", | ||
WaffleLapkin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
vec![ | ||
(expr.span.shrink_to_lo(), format!("(&{mutbl}")), | ||
(expr.span.shrink_to_hi(), ")".to_owned()) | ||
], | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// run-rustfix | ||
use std::ptr::{addr_of, addr_of_mut}; | ||
|
||
unsafe fn _test_mut(ptr: *mut [u8]) -> *mut [u8] { | ||
addr_of_mut!((&mut (*ptr))[..16]) | ||
//~^ error: implicit auto-ref creates a reference to a dereference of a raw pointer | ||
} | ||
|
||
unsafe fn _test_const(ptr: *const [u8]) -> *const [u8] { | ||
addr_of!((&(*ptr))[..16]) | ||
//~^ error: implicit auto-ref creates a reference to a dereference of a raw pointer | ||
} | ||
|
||
fn main() {} |
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,14 @@ | ||
// run-rustfix | ||
use std::ptr::{addr_of, addr_of_mut}; | ||
|
||
unsafe fn _test_mut(ptr: *mut [u8]) -> *mut [u8] { | ||
addr_of_mut!((*ptr)[..16]) | ||
//~^ error: implicit auto-ref creates a reference to a dereference of a raw pointer | ||
} | ||
|
||
unsafe fn _test_const(ptr: *const [u8]) -> *const [u8] { | ||
addr_of!((*ptr)[..16]) | ||
//~^ error: implicit auto-ref creates a reference to a dereference of a raw pointer | ||
} | ||
|
||
fn main() {} |
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,27 @@ | ||
error: implicit auto-ref creates a reference to a dereference of a raw pointer | ||
--> $DIR/implicit_unsafe_autoref.rs:5:18 | ||
| | ||
LL | addr_of_mut!((*ptr)[..16]) | ||
| ^^^^^^ | ||
| | ||
= note: creating a reference inflicts a lot of safety requirements | ||
WaffleLapkin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
= note: `#[deny(implicit_unsafe_autorefs)]` on by default | ||
help: if this reference is intentional, make it explicit | ||
| | ||
LL | addr_of_mut!((&mut (*ptr))[..16]) | ||
| +++++ + | ||
|
||
error: implicit auto-ref creates a reference to a dereference of a raw pointer | ||
--> $DIR/implicit_unsafe_autoref.rs:10:14 | ||
| | ||
LL | addr_of!((*ptr)[..16]) | ||
| ^^^^^^ | ||
| | ||
= note: creating a reference inflicts a lot of safety requirements | ||
help: if this reference is intentional, make it explicit | ||
| | ||
LL | addr_of!((&(*ptr))[..16]) | ||
| ++ + | ||
|
||
error: aborting due to 2 previous errors | ||
|
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.