-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add new lint
as_underscore_ptr
to check for as *{const,mut} _
- Loading branch information
1 parent
58fb801
commit 033da3c
Showing
38 changed files
with
262 additions
and
45 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, MutTy, Ty, TyKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_middle::ty; | ||
|
||
use super::AS_UNDERSCORE_PTR; | ||
|
||
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, mut ty: &Ty<'_>) { | ||
if let TyKind::Ptr(MutTy { mutbl, .. }) = ty.kind { | ||
// get this before stripping the pointers, so the suggestion suggests replacing the whole type | ||
let ty_span = ty.span; | ||
|
||
// strip all pointers from the type | ||
while let TyKind::Ptr(MutTy { ty: new_ty, .. }) = ty.kind { | ||
ty = new_ty; | ||
} | ||
|
||
if matches!(ty.kind, TyKind::Infer) { | ||
let mutbl_str = match mutbl { | ||
rustc_ast::Mutability::Not => "const", | ||
rustc_ast::Mutability::Mut => "mut", | ||
}; | ||
span_lint_and_then( | ||
cx, | ||
AS_UNDERSCORE_PTR, | ||
expr.span, | ||
format!("using `as *{mutbl_str} _` conversion").as_str(), | ||
|diag| { | ||
let ty_resolved = cx.typeck_results().expr_ty(expr); | ||
if let ty::Error(_) = ty_resolved.kind() { | ||
diag.help("consider giving the type explicitly"); | ||
} else { | ||
diag.span_suggestion( | ||
ty_span, | ||
"consider giving the type explicitly", | ||
ty_resolved, | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
}, | ||
); | ||
} | ||
} else { | ||
// not a pointer | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// run-rustfix | ||
|
||
#![allow(unused)] | ||
#![deny(clippy::as_underscore_ptr)] | ||
|
||
struct UwU; | ||
|
||
impl UwU { | ||
fn as_ptr(&self) -> *const u8 { | ||
&self as *const &UwU as *const u8 | ||
} | ||
} | ||
|
||
fn use_ptr(_: *const ()) {} | ||
|
||
fn main() { | ||
let _: *const u8 = 1 as *const u8; | ||
use_ptr(1 as *const ()); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// run-rustfix | ||
|
||
#![allow(unused)] | ||
#![deny(clippy::as_underscore_ptr)] | ||
|
||
struct UwU; | ||
|
||
impl UwU { | ||
fn as_ptr(&self) -> *const u8 { | ||
&self as *const _ as *const u8 | ||
} | ||
} | ||
|
||
fn use_ptr(_: *const ()) {} | ||
|
||
fn main() { | ||
let _: *const u8 = 1 as *const _; | ||
use_ptr(1 as *const _); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
error: using `as *const _` conversion | ||
--> $DIR/as_underscore_ptr.rs:10:9 | ||
| | ||
LL | &self as *const _ as *const u8 | ||
| ^^^^^^^^^-------- | ||
| | | ||
| help: consider giving the type explicitly: `*const &UwU` | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/as_underscore_ptr.rs:4:9 | ||
| | ||
LL | #![deny(clippy::as_underscore_ptr)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: using `as *const _` conversion | ||
--> $DIR/as_underscore_ptr.rs:17:24 | ||
| | ||
LL | let _: *const u8 = 1 as *const _; | ||
| ^^^^^-------- | ||
| | | ||
| help: consider giving the type explicitly: `*const u8` | ||
|
||
error: using `as *const _` conversion | ||
--> $DIR/as_underscore_ptr.rs:18:13 | ||
| | ||
LL | use_ptr(1 as *const _); | ||
| ^^^^^-------- | ||
| | | ||
| help: consider giving the type explicitly: `*const ()` | ||
|
||
error: aborting due to 3 previous errors | ||
|
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.