Skip to content

Make ops::DerefMut a diagnostic item#147643

Closed
relaxcn wants to merge 1 commit intorust-lang:masterfrom
relaxcn:DerefMut
Closed

Make ops::DerefMut a diagnostic item#147643
relaxcn wants to merge 1 commit intorust-lang:masterfrom
relaxcn:DerefMut

Conversation

@relaxcn
Copy link
Copy Markdown
Contributor

@relaxcn relaxcn commented Oct 13, 2025

I'm working on an enhancement to clippy::explicit_deref_methods, which would allow explicit deref or deref_mut method calls in implementation of the the Deref or DerefMut trait, so I need a DerefMut diagnostic item to check if we are already in the impl of DerefMut trait.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 13, 2025
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Oct 13, 2025

r? @lcnr

rustbot has assigned @lcnr.
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

@hkBst
Copy link
Copy Markdown
Member

hkBst commented Oct 13, 2025

@relaxcn Why would it be a good idea to allow such calls in Deref(Mut) impls?

@relaxcn
Copy link
Copy Markdown
Contributor Author

relaxcn commented Oct 13, 2025

@relaxcn Why would it be a good idea to allow such calls in Deref(Mut) impls?

Reference to rust-lang/rust-clippy#15392, I think it is make sense to me.

@@ -266,6 +266,7 @@ impl<T: ?Sized> const Deref for &mut T {
#[lang = "deref_mut"]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

DerefMut already has a lang item, can you use LangItem::DeferMut instead?

Copy link
Copy Markdown
Contributor Author

@relaxcn relaxcn Oct 14, 2025

Choose a reason for hiding this comment

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

Of course. I can use it to archive the same goal.

But strangely enough, Deref is both a language item and a diagnostic item:

#[lang = "deref"]
#[doc(alias = "*")]
#[doc(alias = "&*")]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_diagnostic_item = "Deref"]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
pub const trait Deref: PointeeSized {

Why not the DerefMut to be a diagnostic item? Then we can use them uniformly like:

[sym::Deref, sym::DerefMut]
    .iter()
    .any(|&sym| cx.tcx.is_diagnostic_item(sym, trait_id))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this feels like a reason to remove the Deref diagnostics item as well 😆

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

😂 I'm just confused about it.

Is it bad to use diagnostics item?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

no worries

I am actually not quite sure. Our general approach is to use lang_items instead of diagnostics items if they already exist.

I don't think there's a large reason for that and we may even want is_diagnostics_item to also just lookup lang items 😁 actually, I think that would be a good improvement

The main reason seems to be "there should only be one consistent way to do things"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your explanation.

So I will close this PR.

BTW it will have a big influence if we remove Deref diagnostics item because rust-clippy use it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we have clippy as a subtree, so we can update it in the same PR

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

All right. Thank you.

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

Labels

T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants