-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
feature: add new lint pub_underscore_fields
#10283
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @flip1995 (or someone else) soon. Please see the contribution instructions for more information. |
☔ The latest upstream changes (presumably #10392) made this pull request unmergeable. Please resolve the merge conflicts. |
b134fe6
to
4313a52
Compare
Hey @flip1995, if you wouldn't mind taking a look at this when you get the chance, it would be much appreciated! No rush though :) |
☔ The latest upstream changes (presumably #10481) made this pull request unmergeable. Please resolve the merge conflicts. |
@ParkMyCar Philipp stopped being on the reviewer rotation a few months ago, are you still interested in continuing with this development? ฅ^•ﻌ•^ฅ r? @blyxyas |
4313a52
to
f53c6be
Compare
For sure! I just rebased off of master and updated the lint version, whenever you have time let me know if there is any other feedback :) |
f53c6be
to
da69c5e
Compare
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.
Very good for a first review! Just a couple minor changes and I think this is it ❤️ |ΦωΦ|
return; | ||
}; | ||
|
||
let msg = "field marked as public but also inferred as unused because it's prefixed with `_`"; |
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.
I'd just inline the message, but if you want to have it as a separate variable, put it later in the checks (just before span_lint_and_help
). We don't want to allocate innecesarily.
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.
Inlined it is :)
}; | ||
|
||
let msg = "field marked as public but also inferred as unused because it's prefixed with `_`"; | ||
for field in st.fields().iter() { |
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.
for field in st.fields().iter() { | |
for field in st.fields() { |
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.
Done!
It would be good if this ignored |
Thanks for the feedback @Alexendoo! I'm not sure I agree though. AFAIU prefixing a field name with an underscore generally indicates that field should be ignored, if some part of your code needs to use the |
c283906
to
3d16f47
Compare
On the topic of |
@@ -20,9 +20,15 @@ declare_clippy_lint! { | |||
/// ``` | |||
/// Use instead: | |||
/// ```rust | |||
/// struct FileHandle { | |||
/// struct FileHandle_Foo { |
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.
Mixing PascalCase with underscores hehe 🐈
For future reference, you can use attributes like should_panic
on your code blocks so that these name collisions don't matter.
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.
You can make it two separate code blocks
```rust
struct FileHandle { .. }
```
or
```rust
struct FileHandle { .. }
```
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.
🤦♂️ yep totally missed this, updated it to be two code blocks!
e.g. https://docs.rs/enumset/1.1.2/src/enumset/set.rs.html#125-130 |
@blyxyas @Alexendoo thanks for the examples! I'm thoroughly convinced, I updated the lint to be a LatePass so we can get type info. Then skip fields that are I struggled a bit to get the visibility settings right, if there's an easier to detect the range of "pub" visibilities we want to guard against, please let me know! |
Whoops, just realized I forgot to add tests for the new |
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.
Just some polishing and I think this is ready! Thanks for the patience! ❤️ (=^・ω・^=)
let is_visible = |field: &FieldDef<'_>| { | ||
let parent = cx.tcx.parent_module_from_def_id(field.def_id); | ||
let grandparent = cx.tcx.parent_module_from_def_id(parent.into()); | ||
let visibility = cx.tcx.visibility(field.def_id); | ||
|
||
let case_1 = parent == grandparent && !field.vis_span.is_empty(); | ||
let case_2 = visibility != Visibility::Restricted(parent.to_def_id()); | ||
|
||
case_1 || case_2 | ||
}; |
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.
Can try cx.effective_visibilities.is_exported(field.def_id))
for this
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.
I tried that, unfortunately it didn't work :/
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.
I think the issue is the test file, none of the fields in it are actually publicly reachable. You'd have to move the definitions outside of fn main() {}
and make the inner
/inner2
modules public
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.
is_exported
doesn't lint for pub(...)
, and it seems like it has some problems with fields of type Option<T>
for some reason?
I don't see it as a requirement.
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.
The effective visibility is what matters for the lint though
Fields prefixed with an
_
are inferred as unused, which suggests it should not be marked aspub
, because marking it aspub
infers it will be used.
pub(crate)
, or pub
where the field is not publicly reachable does not mark the field as used
What's the issue with Option
fields?
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.
Hmm, now I'm not sure.
We could lint for any field that is public and its name starts with _
, which is what the name of lint implies. Or we could analyze the description a little bit more and just lint when the pub
is actually useful.
Sweet! Made all of your suggested changes! |
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.
Just the version upgrade and that's all, thanks ❤️!
Could you please rebase and squash these commits into 1 or 2 relevant ones? |
cb98e49
to
c9a2555
Compare
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.
And also, could you run cargo collect-metadata
ea82130
to
73a3636
Compare
Updated to 1.77, ran |
book/src/lint_configuration.md
Outdated
--- | ||
**Affected lints:** | ||
* [`pub_underscore_fields`](https://rust-lang.github.io/rust-clippy/master/index.html#pub_underscore_fields) | ||
* [`or`](https://rust-lang.github.io/rust-clippy/master/index.html#or) |
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.
I'm not really sure why this is here
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.
Me either! It's what cargo collect-metadata
generated, other lints seem to have similar "Affected lints" sections :)
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.
I meant the or
lint, that doesn't exist xD
@bors try |
feature: add new lint `pub_underscore_fields` fixes: #10282 This PR introduces a new lint `pub_underscore_fields` that lints when a user has marked a field of a struct as public, but also prefixed it with an underscore (`_`). This is something users should avoid because the two ideas are contradictory. Prefixing a field with an `_` is inferred as the field being unused, but making a field public infers that it will be used. - \[x] Followed [lint naming conventions][lint_naming] - I believe I followed the naming conventions, more than happy to update the naming if I did not :) - \[x] Added passing UI tests (including committed `.stderr` file) - \[x] `cargo test` passes locally - \[x] Executed `cargo dev update_lints` - \[x] Added lint documentation - \[x] Run `cargo dev fmt` --- changelog: new lint: [`pub_underscore_fields`] [#10283](#10283) <!-- changelog_checked -->
☀️ Try build successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
- add a new late pass lint, with config options - add ui tests for both variations of config option - update CHANGELOG.md github feedback bump version to 1.77 and run cargo collect-metadata Change `,` to `;` in `conf.rs`
7df1b40
to
fa7dd1c
Compare
I changed that Let's hope that everything goes well this time (=^・^=) |
@bors r+ |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
Woo! Thank you @blyxyas for reviewing this PR and getting it merged, I appreciate it! 😊 |
It's spelled "publicly", not "publically". I recommend fixing that in the Furthermore, the |
I'll fix this myself. Thanks for the report. |
Add . to end of lint lists in configuration + Fix typo in pub_underscore_fields_behavior Fixes #10283 (comment) In the "/// Lint: " list on each configuration option, you have to end with a dot. If the lint list doesn't have a dot, the configuration won't have documentation. This PR adds those missing dots in some of the configuration, thus also adding their documentation. changelog: Fix bug where a lot of config documentation wasn't showing. changelog: Fix typo in `pub_underscore_fields_behavior` (`PublicallyExported` -> `PubliclyExported`)
Add . to end of lint lists in configuration + Fix typo in pub_underscore_fields_behavior Fixes #10283 (comment) In the "/// Lint: " list on each configuration option, you have to end with a dot. If the lint list doesn't have a dot, the configuration won't have documentation. This PR adds those missing dots in some of the configuration, thus also adding their documentation. changelog: Fix bug where a lot of config documentation wasn't showing. changelog: Fix typo in `pub_underscore_fields_behavior` (`PublicallyExported` -> `PubliclyExported`)
Add . to end of lint lists in configuration + Fix typo in pub_underscore_fields_behavior Fixes #10283 (comment) In the "/// Lint: " list on each configuration option, you have to end with a dot. If the lint list doesn't have a dot, the configuration won't have documentation. This PR adds those missing dots in some of the configuration, thus also adding their documentation. changelog: Fix bug where a lot of config documentation wasn't showing. changelog: Fix typo in `pub_underscore_fields_behavior` (`PublicallyExported` -> `PubliclyExported`)
Add . to end of lint lists in configuration + Fix typo in pub_underscore_fields_behavior Fixes #10283 (comment) In the "/// Lint: " list on each configuration option, you have to end with a dot. If the lint list doesn't have a dot, the configuration won't have documentation. This PR adds those missing dots in some of the configuration, thus also adding their documentation. changelog: Fix bug where a lot of config documentation wasn't showing. changelog: Fix typo in `pub_underscore_fields_behavior` (`PublicallyExported` -> `PubliclyExported`)
fixes: #10282
This PR introduces a new lint
pub_underscore_fields
that lints when a user has marked a field of a struct as public, but also prefixed it with an underscore (_
). This is something users should avoid because the two ideas are contradictory. Prefixing a field with an_
is inferred as the field being unused, but making a field public infers that it will be used..stderr
file)cargo test
passes locallycargo dev update_lints
cargo dev fmt
changelog: new lint: [
pub_underscore_fields
]#10283