Skip to content

Conversation

@zihan0822
Copy link
Contributor

@zihan0822 zihan0822 commented Aug 5, 2025

Don't emit machine applicable suggestion for unsuggestable inferred type, for example fn item pointer.
Fixes: #15281

changelog: [as_pointer_underscore]: reduce applicability for unsuggestable inferred type

@rustbot
Copy link
Collaborator

rustbot commented Aug 5, 2025

r? @Alexendoo

rustbot has assigned @Alexendoo.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Aug 5, 2025
@zihan0822 zihan0822 force-pushed the fix-as-pointer-underscore branch from 6e3f3e5 to a53cbdc Compare August 5, 2025 17:47
@zihan0822
Copy link
Contributor Author

r? clippy

@rustbot rustbot assigned y21 and unassigned Alexendoo Aug 24, 2025
Comment on lines 8 to 10
&& let ty::RawPtr(pointee, _) = ty_into.kind()
// skip fn item type, since it can not be explicitly expressed with syntax
&& !matches!(pointee.kind(), ty::FnDef(..))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The missing_transmute_annotations lint was running into basically the same unnamed types issue and someone wrote a function for it:

fn ty_cannot_be_named(ty: Ty<'_>) -> bool {
matches!(
ty.kind(),
ty::Alias(ty::AliasTyKind::Opaque | ty::AliasTyKind::Inherent, _)
)
}

Could make sense to add ty::FnDef(..) there and use that function here as well so we have one place for checking for whether a type could be written in user code. What do you think?

Copy link
Member

@y21 y21 Sep 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And looking at how this situation is handled in missing_transmute_annotations, it might make sense to handle it in the same way here and actually still emit a warning with a reduced applicability (not MachineApplicable).

I can't imagine a situation in which one would want to create a raw pointer to a fn item, but given that this is a restriction lint that one would have to enable manually, it does seem valid to emit a warning anyway even if it can't simply be written because the point still stands that as *const _ is prone to refactoring bugs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could make sense to add ty::FnDef(..) there and use that function here as well so we have one place for checking for whether a type could be written in user code. What do you think?

makes sense. just moved ty_cannot_be_named to clippy_utils.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense. just moved ty_cannot_be_named to clippy_utils.

is_suggestable function used in #15652 might be a better thing to do here? Probably we can update missing_transmute_annotations as well in a separate PR.

use rustc_middle::ty::IsSuggestable;
use super::AS_UNDERSCORE;
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, ty: &'tcx Ty<'_>) {
if matches!(ty.kind, TyKind::Infer(())) {
span_lint_and_then(cx, AS_UNDERSCORE, expr.span, "using `as _` conversion", |diag| {
let ty_resolved = cx.typeck_results().expr_ty(expr);
if ty_resolved.is_suggestable(cx.tcx, true) {
diag.span_suggestion(

@zihan0822 zihan0822 force-pushed the fix-as-pointer-underscore branch from a53cbdc to 2f1aa60 Compare September 17, 2025 05:31
@rustbot
Copy link
Collaborator

rustbot commented Sep 17, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@zihan0822 zihan0822 force-pushed the fix-as-pointer-underscore branch from 2f1aa60 to 3a55d66 Compare September 17, 2025 05:43
@zihan0822 zihan0822 changed the title as_pointer_underscore: don't lint when casting fn item pointers as_pointer_underscore: reduce applicability for unnameable inferred type Sep 17, 2025
@zihan0822 zihan0822 force-pushed the fix-as-pointer-underscore branch from 3a55d66 to 20afee5 Compare September 17, 2025 18:42
don't emit machine applicable suggestion when the inferred type is
unsuggestable, for example fn item pointer, closure

changelog: [`as_pointer_underscore`]: reduce applicability for
unsuggestable inferred type

Signed-off-by: Zihan <[email protected]>
@zihan0822 zihan0822 force-pushed the fix-as-pointer-underscore branch from 20afee5 to 45b4531 Compare September 17, 2025 18:55
@zihan0822 zihan0822 requested a review from y21 September 17, 2025 19:10
@zihan0822
Copy link
Contributor Author

ping @y21 :)

Comment on lines +2 to +3
#![crate_type = "lib"]
#![no_std]
Copy link
Member

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?

Copy link
Member

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.rs test 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.

Copy link
Contributor Author

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

as-pointer-underscore suggests *const fn() -> u32 {foo}

4 participants