Add a new lint UNUSED_UNCONSTRUCTABLE_PUB_STRUCTS#146440
Conversation
|
r? @davidtwco rustbot has assigned @davidtwco. Use |
UNCONSTRUCTIBLE_PUB_STRUCT to detect unconstructible public structs
|
Would the lint fire on token structs that are public, have private fields, have no public constructor method, but expose a limited number of pre-constructed objects, e.g. through a static that contains an optional token? |
won't fire like private types used in such places |
480b1d7 to
021712b
Compare
|
Nominating for t-lang to decide whether we want this lint, then I'll review the implementation. Also, s/unconstructible/unconstructable. |
UNCONSTRUCTIBLE_PUB_STRUCT to detect unconstructible public structsUNCONSTRUCTABLE_PUB_STRUCT to detect unconstructable public structs
This comment was marked as resolved.
This comment was marked as resolved.
6075d81 to
497ad71
Compare
This comment has been minimized.
This comment has been minimized.
This comment was marked as resolved.
This comment was marked as resolved.
497ad71 to
b3b3a05
Compare
|
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. |
|
Does this trigger on structs that are intended only as marker types in generic parameters? |
Such types usually have intended private unit fields and this won't trigger on them. This will only trigger types with trivial fields but not be used or cannot be constructed. |
|
Are there examples of code in the wild that is affected by this lint? |
|
I worry about completeness here. If I have something like Musing: what if this was signature-based, say? Could it be phrased as "why is this |
|
We discussed this in today's @rust-lang/lang meeting. Was there any particular motivating use case that led to proposing this? Can you point to some code that motivated this? We wondered about potential corner cases, notably structs that are only constructed in unsafe code. For instance, something using Once those are addressed, we'd like to see the (triaged) results of a crater run with this lint marked as deny-by-default, so we can get an idea of 1) how widespread this is and 2) whether this catches any issues. |
|
Please renominate for lang when these answers are available. |
b3b3a05 to
a1aaeb6
Compare
|
☀️ Try build successful (CI) |
|
@craterbot check |
|
👌 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
|
🚧 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
|
🎉 Experiment
Footnotes
|
This comment has been minimized.
This comment has been minimized.
|
Sorry for the long delay in getting back to this. I previously hadn’t quite figured out how to make the new lint After the implementation of lint
First, to clarify: we currently don't have a
A type is considered an unconstructable candidate only if:
Then we also exclude a few intentional/special cases, the following kinds of structs won't be checked:
The rules above should cover the sample here, as well as other common cases of intentionally unconstructable types.
I did a crater run (#146440 (comment)), but it produced too many results, so that I cannot check them all one by one in fact. I checked some of them (though not many), and did not observe false-positives. The types were either unused in fact, or only used under certain After all, I think this lint is valuable for detecting and eliminating unused Also, although we already have tools for finding unused |
|
What do you make of the arti case?
What are your thoughts on whether this should be widened to exempt all uninhabited types (rather than just types containing a field of never type)? What about to all types containing only ZST fields (rather than just all- |
Good catch! I missed it. I believed it is not expected because
Widening the exemption to all uninhabited types makes sense to me.
After thinking about it, I think this also makes sense. We may have custom Thanks for your suggestions! Updated the implementation and exemption rules in #146440 (comment). |
|
@bors try TODO: |
This comment has been minimized.
This comment has been minimized.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
|
@craterbot run mode=check-only crates=https://crater-reports.s3.amazonaws.com/pr-146440/retry-regressed-list.txt p=1 |
|
👌 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
|
🚧 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
|
🎉 Experiment
Footnotes
|
I'm curious if you could elaborate on this a bit. Sitting with it,
I see the appeal in the latter, but constructible is the more canonical form. See the ngrams viewer. |
What in my mind was that But I would agree that another explanation is that this lint strips out the ones used in local crate. Then it could keep being
Cool! I'll rename it once lint can be merged in and the name is settled. |
|
Reviewing #146440 (comment), including the subsequent adjustments in #146440 (comment), this looks correct and desirable to me. There's a set of careful mitigations here for the cases we had raised and that were found in the crater run. I don't see anything further to do here. Proposal: Let's do this, as warn-by-default, with the name @rfcbot fcp merge lang |
|
Team member @traviscross has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns. |
View all comments
Add a new lint
UNUSED_UNCONSTRUCTABLE_PUB_STRUCTSto detect unused unconstructable public structs, based on the following observations:And, with the similar approach of the lint
dead_code_pub_in_binary, this lint is also independent ofdead_codeanddead_code_pub_in_binary.