Skip to content
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

Add diagnostic items for Clippy #87170

Merged
merged 3 commits into from
Jul 18, 2021

Conversation

xFrednet
Copy link
Member

This adds a bunch of diagnostic items to std/core/alloc functions, structs and traits used in Clippy. The actual refactorings in Clippy to use these items will be done in a different PR in Clippy after the next sync.

This PR doesn't include all paths Clippy uses, I've only gone through the first 85 lines of Clippy's paths.rs (after rust-lang/rust-clippy#7466) to get some feedback early on. I've also decided against adding diagnostic items to methods, as it would be nicer and more scalable to access them in a nicer fashion, like adding a is_diagnostic_assoc_item(did, sym::Iterator, sym::map) function or something similar (Suggested by @camsteffen on Zulip)

There seems to be some different naming conventions when it comes to diagnostic items, some use UpperCamelCase (BinaryHeap) and some snake_case (hashmap_type). This PR uses UpperCamelCase for structs and traits and snake_case with the module name as a prefix for functions. Any feedback on is this welcome.

cc: rust-lang/rust-clippy#5393

r? @Manishearth

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 15, 2021
@xFrednet xFrednet changed the title Add more diagnostic items for Clippy Add diagnostic items for Clippy Jul 15, 2021
library/std/src/io/mod.rs Outdated Show resolved Hide resolved
library/core/src/mem/mod.rs Outdated Show resolved Hide resolved
@rust-log-analyzer

This comment has been minimized.

@xFrednet
Copy link
Member Author

The pipeline shows why some diagnostic items use #[cfg_attr(not(test), rustc_diagnostic_item = "...")]. I would apply the fixes and your suggestions in the two commits and force push the changes again. Would that be okay for you @Manishearth

@camsteffen
Copy link
Contributor

I would just add diagnostic items for modules cmp, mem and iter. We can use tcx.parent to check the containing module of a function DefId (and possibly add utils to make it easier).

Aside: I am looking into adding a new lookup util so we can write code like lookup!(cmp::min).has_def_id(id).

@xFrednet
Copy link
Member Author

xFrednet commented Jul 15, 2021

Wouldn't adding diagnostic items to the modules kind of defeat the purpose of using diagnostic items instead of paths? We wouldn't match against the whole path, but it seems weird 🤔
On second thought, it would make sense if we would have a function like is_diagnostic_assoc_item then it would basically be the same as asking for a method on a struct/trait. Do we still want to add them in this PR in the meantime?

Such a lookup macro would be awesome, btw.

@Manishearth
Copy link
Member

I would just add diagnostic items for modules cmp, mem and iter. We can use tcx.parent to check the containing module of a function DefId (and possibly add utils to make it easier).

Hmm, but doesn't that bring us closer to what we used to have? Seems faster and cleaner to just have diagnostic items for everyone.

The pipeline shows why some diagnostic items use #[cfg_attr(not(test), rustc_diagnostic_item = "...")]. I would apply the fixes and your suggestions in the two commits and force push the changes again. Would that be okay for you @Manishearth

Yeah, sure!

@Manishearth
Copy link
Member

cc @estebank

@xFrednet xFrednet force-pushed the clippy-5393-add-diagnostic-items branch from 22e152c to 3d5453f Compare July 15, 2021 21:52
@@ -1829,6 +1829,7 @@ impl<K, V, S> Debug for RawEntryBuilder<'_, K, V, S> {
///
/// [`entry`]: HashMap::entry
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "HashmapEntry")]
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
#[cfg_attr(not(test), rustc_diagnostic_item = "HashmapEntry")]
#[cfg_attr(not(test), rustc_diagnostic_item = "HashMapEntry")]

@xFrednet xFrednet force-pushed the clippy-5393-add-diagnostic-items branch from 3d5453f to 37626f3 Compare July 15, 2021 21:56
@xFrednet xFrednet force-pushed the clippy-5393-add-diagnostic-items branch from 37626f3 to d38f2b0 Compare July 15, 2021 21:57
@camsteffen
Copy link
Contributor

I recommend that only because some functions are very closely tied to the module they are in. min will always be cmp::min. This is reflected in that you choose to name it cmp_min. This approach means adding a lot less diagnostic items and less going to check what the diagnostic item is for specific items. It's analogous to not adding a diagnostic item for every Iterator method. We could have a util method like is_diag_child_named for now. It would be more frictionless with my lookup! idea.

@rust-log-analyzer

This comment has been minimized.

Copy link
Member

@Manishearth Manishearth left a comment

Choose a reason for hiding this comment

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

Maybe someone from @rust-lang/compiler can make sure we're not doing anything wrong here

@camsteffen
Copy link
Contributor

It would work with associated types, associated methods, inherent methods, module items, ...

lookup!(HashMap)
lookup!(HashMap::Entry)
lookup!(HashMap::insert)
lookup!(Iterator::find)
lookup!(cmp::min)

@xFrednet
Copy link
Member Author

I definitely like the idea. Note that it probably wouldn't work for HashMap::Entry as that type is separate from the HashMap struct itself (This was probably just an example).

Using the parent search method would probably still not fully work for reexports though. std::iter::repeat for instance, has the actual path core::iter::sources::repeat::repeat adding a diagnostic item to the iter module would only drop the first core:: part of it

@Manishearth
Copy link
Member

Right, part of the reason behind having diagnostic items is never having to know the internal organization of the crate. It makes sense to do parent lookups for methods/etc but not as much for free functions

@oli-obk
Copy link
Contributor

oli-obk commented Jul 16, 2021

@bors r=Manishearth,oli-obk

@bors
Copy link
Contributor

bors commented Jul 16, 2021

📌 Commit d38f2b0 has been approved by Manishearth,oli-obk

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 16, 2021
@oli-obk
Copy link
Contributor

oli-obk commented Jul 16, 2021

@xFrednet
Copy link
Member Author

xFrednet commented Jul 16, 2021

Right, part of the reason behind having diagnostic items is never having to know the internal organization of the crate. It makes sense to do parent lookups for methods/etc but not as much for free functions

Would it maybe be worth it to implement a function/macro like lookup! that doesn't use diagnostic items at all and instead resolves the items like use statements do?

My understanding is that paths in use statements resolve to the original DefId even if the item has been reexported. This would allow the usage of these for diagnostics without knowing the internal design of these crates. Example: lookup("std::iter::repeat") would return the DefIdfor core::iter::sources::repeat::repeat as that is also the function that is being called when the user uses use std::iter::repeat.

Has there been any previous discussions about a concept like this?

Edit: @oli-obk thanks for posting the link again, should we move the discussion over to Zulip? 🙃

@xFrednet
Copy link
Member Author

@Manishearth
Copy link
Member

The point of diagnostic items was, in part, to not have to do any such lookups: I don't see what problem it solves to switch from diagnostic items to lookups.

@xFrednet
Copy link
Member Author

My understanding was that the main point of diagnostic items was to avoid hard coded paths to the exact item. That problem would be solved, and it would make a slightly nicer interface IMO. But that's it.

There has also been some feedback on Zulip that using the resolver for this can be problematic and produce ICEs. I'll dig a bit more through the compiler, but it seems like diagnostic items are a currently the best solution.

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Jul 16, 2021
…-items, r=Manishearth,oli-obk

Add diagnostic items for Clippy

This adds a bunch of diagnostic items to `std`/`core`/`alloc` functions, structs and traits used in Clippy. The actual refactorings in Clippy to use these items will be done in a different PR in Clippy after the next sync.

This PR doesn't include all paths Clippy uses, I've only gone through the first 85 lines of Clippy's [`paths.rs`](https://github.com/rust-lang/rust-clippy/blob/ecf85f4bdc319f9d9d853d1fff68a8a25e64c7a8/clippy_utils/src/paths.rs) (after rust-lang/rust-clippy#7466) to get some feedback early on. I've also decided against adding diagnostic items to methods, as it would be nicer and more scalable to access them in a nicer fashion, like adding a `is_diagnostic_assoc_item(did, sym::Iterator, sym::map)` function or something similar (Suggested by `@camsteffen` [on Zulip](https://rust-lang.zulipchat.com/#narrow/stream/147480-t-compiler.2Fwg-diagnostics/topic/Diagnostic.20Item.20Naming.20Convention.3F/near/225024603))

There seems to be some different naming conventions when it comes to diagnostic items, some use UpperCamelCase (`BinaryHeap`) and some snake_case (`hashmap_type`). This PR uses UpperCamelCase for structs and traits and snake_case with the module name as a prefix for functions. Any feedback on is this welcome.

cc: rust-lang/rust-clippy#5393

r? `@Manishearth`
@GuillaumeGomez
Copy link
Member

@bors: r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 16, 2021
@xFrednet
Copy link
Member Author

xFrednet commented Jul 17, 2021

Sorry, that I didn't fix the error earlier, the error message confused me at first. Now everything should hopefully be correct 🙃

@oli-obk
Copy link
Contributor

oli-obk commented Jul 17, 2021

@bors r=Manishearth,oli-obk

@bors
Copy link
Contributor

bors commented Jul 17, 2021

📌 Commit 67002db has been approved by Manishearth,oli-obk

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 17, 2021
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 18, 2021
Rollup of 8 pull requests

Successful merges:

 - rust-lang#86763 (Add a regression test for issue-63355)
 - rust-lang#86814 (Recover from a misplaced inner doc comment)
 - rust-lang#86843 (Check that const parameters of trait methods have compatible types)
 - rust-lang#86889 (rustdoc: Cleanup ExternalCrate)
 - rust-lang#87092 (Remove nondeterminism in multiple-definitions test)
 - rust-lang#87170 (Add diagnostic items for Clippy)
 - rust-lang#87183 (fix typo in compile_fail doctest)
 - rust-lang#87205 (rustc_middle: remove redundant clone)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 07faa2e into rust-lang:master Jul 18, 2021
@rustbot rustbot added this to the 1.55.0 milestone Jul 18, 2021
@xFrednet xFrednet deleted the clippy-5393-add-diagnostic-items branch August 3, 2021 10:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants