forked from rust-lang/rust-clippy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…3251) changelog: New lint for as *const _ and as *mut _ pointer casts EDIT: lets go with the simpler version
- Loading branch information
Showing
7 changed files
with
98 additions
and
0 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,19 @@ | ||
use rustc_errors::Applicability; | ||
use rustc_lint::LateContext; | ||
use rustc_middle::ty::Ty; | ||
|
||
pub fn check<'tcx>(cx: &LateContext<'tcx>, ty_into: Ty<'_>, cast_to_hir: &'tcx rustc_hir::Ty<'tcx>) { | ||
if let rustc_hir::TyKind::Ptr(rustc_hir::MutTy { ty, .. }) = cast_to_hir.kind | ||
&& matches!(ty.kind, rustc_hir::TyKind::Infer) | ||
{ | ||
clippy_utils::diagnostics::span_lint_and_sugg( | ||
cx, | ||
super::AS_POINTER_UNDERSCORE, | ||
cast_to_hir.span, | ||
"using inferred pointer cast", | ||
"use explicit type", | ||
ty_into.to_string(), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} |
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,15 @@ | ||
#![warn(clippy::as_pointer_underscore)] | ||
#![crate_type = "lib"] | ||
#![no_std] | ||
|
||
struct S; | ||
|
||
fn f(s: &S) -> usize { | ||
&s as *const &S as usize | ||
//~^ ERROR: using inferred pointer cast | ||
} | ||
|
||
fn g(s: &mut S) -> usize { | ||
s as *mut S as usize | ||
//~^ ERROR: using inferred pointer cast | ||
} |
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,15 @@ | ||
#![warn(clippy::as_pointer_underscore)] | ||
#![crate_type = "lib"] | ||
#![no_std] | ||
|
||
struct S; | ||
|
||
fn f(s: &S) -> usize { | ||
&s as *const _ as usize | ||
//~^ ERROR: using inferred pointer cast | ||
} | ||
|
||
fn g(s: &mut S) -> usize { | ||
s as *mut _ as usize | ||
//~^ ERROR: using inferred pointer cast | ||
} |
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,17 @@ | ||
error: using inferred pointer cast | ||
--> tests/ui/as_pointer_underscore.rs:8:11 | ||
| | ||
LL | &s as *const _ as usize | ||
| ^^^^^^^^ help: use explicit type: `*const &S` | ||
| | ||
= note: `-D clippy::as-pointer-underscore` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::as_pointer_underscore)]` | ||
|
||
error: using inferred pointer cast | ||
--> tests/ui/as_pointer_underscore.rs:13:10 | ||
| | ||
LL | s as *mut _ as usize | ||
| ^^^^^^ help: use explicit type: `*mut S` | ||
|
||
error: aborting due to 2 previous errors | ||
|