-
Couldn't load subscription status.
- Fork 1.8k
as_pointer_underscore: reduce applicability for unnameable inferred type
#15418
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
zihan0822
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
zihan0822:fix-as-pointer-underscore
base: master
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
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -1,19 +1,30 @@ | ||
| use rustc_errors::Applicability; | ||
| use rustc_lint::LateContext; | ||
| use rustc_middle::ty::Ty; | ||
| use rustc_middle::ty::{IsSuggestable, Ty}; | ||
|
|
||
| pub fn check<'tcx>(cx: &LateContext<'tcx>, ty_into: Ty<'_>, cast_to_hir: &'tcx rustc_hir::Ty<'tcx>) { | ||
| pub fn check<'tcx>(cx: &LateContext<'tcx>, ty_into: Ty<'tcx>, 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, | ||
| ); | ||
| if ty_into.is_suggestable(cx.tcx, true) { | ||
| 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, | ||
| ); | ||
| } else { | ||
| clippy_utils::diagnostics::span_lint_and_help( | ||
| cx, | ||
| super::AS_POINTER_UNDERSCORE, | ||
| cast_to_hir.span, | ||
| format!("using inferred pointer cast to unsuggestable type: `{ty_into}`"), | ||
| None, | ||
| "use explicit type", | ||
| ); | ||
| } | ||
| } | ||
| } |
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 @@ | ||
| #![warn(clippy::as_pointer_underscore)] | ||
| #![crate_type = "lib"] | ||
| #![no_std] | ||
|
|
||
| fn issue_15281() { | ||
| fn bar(_: usize) {} | ||
| // pointer to fn item, lint should not trigger | ||
| let _ = &bar as *const _; | ||
| //~^ as_pointer_underscore | ||
|
|
||
| let closure = &|| {}; | ||
| let _ = &closure as *const _; | ||
| //~^ as_pointer_underscore | ||
| } | ||
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,20 @@ | ||
| error: using inferred pointer cast to unsuggestable type: `*const fn(usize) {issue_15281::bar}` | ||
| --> tests/ui/as_pointer_underscore_unfixable.rs:8:21 | ||
| | | ||
| LL | let _ = &bar as *const _; | ||
| | ^^^^^^^^ | ||
| | | ||
| = help: use explicit type | ||
| = 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 to unsuggestable type: `*const &{closure@tests/ui/as_pointer_underscore_unfixable.rs:11:20: 11:22}` | ||
| --> tests/ui/as_pointer_underscore_unfixable.rs:12:25 | ||
| | | ||
| LL | let _ = &closure as *const _; | ||
| | ^^^^^^^^ | ||
| | | ||
| = help: use explicit type | ||
|
|
||
| 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two attrs should be redundant, or are they needed for something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, could the single test case in this file just be part of
tests/ui/as_pointer_underscore.rs? Usually_unfixable.rstest files are for lints that emit a broken suggestion and need//@no-rustfix, but in this case there's no suggestion at all so it should be fine to live in the same file as the other lint's test cases.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are actually still emitting some helper message w/o the full type name: "use explicit type" for unsuggestable types. Should we make it silent?