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 lint_groups_priority lint #11832

Merged
merged 1 commit into from
Jan 31, 2024
Merged

Conversation

Alexendoo
Copy link
Member

Warns when a lint group in Cargo.toml's [lints] section shares the same priority as a lint. This is in the cargo section but is categorised as correctness so it's on by default, it doesn't call cargo metadata though and parses the Cargo.toml directly

The lint should be temporary until rust-lang/cargo#12918 is resolved, but in the meanwhile this is an common issue to run into

changelog: Add [lint_groups_priority] lint

r? @flip1995

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Nov 17, 2023
@Alexendoo Alexendoo force-pushed the lint-groups-priority branch 2 times, most recently from 6c3e399 to 2811442 Compare November 17, 2023 14:56
@flip1995
Copy link
Member

flip1995 commented Nov 17, 2023

Ugh, I really dislike re-parsing the Cargo.toml file. Especially something as complex as the lint table. This should be extended in the future AFAIK. Keeping up with this might be hard. How much we have to keep up with it depends on how temporary this temporary lint is.

Is there the possibility to forward this through cargo-metadata somehow? But then again, calling cargo-metadata on a enabled-by-default lint can have a huge perf impact, making it not really an option... But then again again, having this lint allow-by-default would be pretty useless.1

The problem with the "temporary" part that I see is: cargo doesn't have the knowledge of what is and isn't a lint group. So moving this lint/warning to cargo seems unrealistic in the near future.

I acknowledge that this is an actual problem with the lint table and that we have to do something about this though.

@epage Asking for your guidance here: Do you have better ideas on how to address this issue in the short-term?

Footnotes

  1. I think there were plans to put the Cargo.toml parsing code in a crate and publish it on crates.io. But I also don't think this will happen any time soon(?)

@epage
Copy link

epage commented Nov 17, 2023

rust-lang/cargo#12918 is our issue for this and it links to the future possibility from the RFC for ways that cargo could solve this

@Alexendoo
Copy link
Member Author

Yeah redefining the lint table isn't ideal, it's defined slightly lax by having [lints.rust] and [lints.clippy] hardcoded, so the addition of other keys that don't correspond to tools in [lints] wouldn't break the lint

It does give nicer diagnostics than the cargo lints that read from cargo metadata though

@flip1995
Copy link
Member

flip1995 commented Nov 20, 2023

@epage I saw that linked in the PR description. My question was more about your expectations on when this will be addressed in cargo. Re-parsing parts of the Cargo.toml file in Clippy is really not ideal. So for me to agree to merge this "temporarily" requires one of two things:

  1. Some approximate timeline on when cargo wants to address this issue; or
  2. Share the Cargo.toml parsing code between Clippy and cargo

The second one would be a long term solution and we could keep this lint permanently in Clippy. But achieving that also seems unrealistic in the short term.

@epage
Copy link

epage commented Nov 20, 2023

For 1, that is too large of an effort for me to slip in among the other work I'm trying to finish up.

For 2, making the schema available as a package is part of that work I'm trying to finish up so crates.io can use it. It won't have any of the other logic in it like filling in of inferred fields or inheriting from workspaces.

@flip1995
Copy link
Member

For 2, making the schema available as a package is part of that work I'm trying to finish up so crates.io can use it.

That's great to hear! In that case, IMO we can merge this now and rework it with the package later. (Maybe together with the other cargo lints we have)

@epage Last request: Can I ask you or someone else from the cargo team to review the toml-parsing code in this PR? I don't want to miss something important there.

Copy link

@epage epage left a comment

Choose a reason for hiding this comment

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

Ill have to check check_table later. As for the serde types, they are more loosely defined which is fine.

Note that this doesn't help with rustdoc.

clippy_lints/src/cargo/mod.rs Outdated Show resolved Hide resolved
Copy link

@epage epage left a comment

Choose a reason for hiding this comment

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

Sorry, lost track of finishing my look at this

@flip1995
Copy link
Member

I saw Ed point out the cargo_utils_schema crate here: #10306 (comment)

Can the lint implementation be updated with this, instead of implementing the parsing logic yourself?

https://docs.rs/cargo-util-schemas/latest/cargo_util_schemas/manifest/struct.TomlManifest.html

@Alexendoo
Copy link
Member Author

I don't think we would be able to get span information from cargo_utils_schema, so we could do it using that but we wouldn't be able to produce nice error messages or have an edit suggestion

@epage
Copy link

epage commented Jan 31, 2024

The straightforward way of getting spans for a toml file is to define serde types with fields wrapped by serde_spanned. This is invasive enough that I'm trying to avoid it for cargo-util-schema.

In cargo, we will be experimenting with a less straightforward approach. You can define a serde deserializer that accepts a path to a field and it will error when it gets to that path. You can then get the span from the error.

Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

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

Alright, I don't want to block this any longer. Sorry for the long time it took me to get back to this. I just have one clarifying question, then this is ready to go.

Comment on lines 165 to 166
// internally warnings is considered a lint rather than a group
rustc_groups.insert("warnings");
Copy link
Member

Choose a reason for hiding this comment

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

Does this matter? I think that you can pass in -Dwarnings anywhere and it won't change the lint level of any other lint you set before/after, as warnings is not actually a group but some special handled keyword/lint.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah looks like it doesn't matter, removed it 👍

@Alexendoo Alexendoo force-pushed the lint-groups-priority branch from 357af18 to 6ffd545 Compare January 31, 2024 18:12
Warns when a lint group in Cargo.toml's `[lints]` section shares the
same priority as a lint
@Alexendoo Alexendoo force-pushed the lint-groups-priority branch from 6ffd545 to 6619e8c Compare January 31, 2024 18:33
@flip1995
Copy link
Member

@bors r+

Thanks! Also a big thanks to Ed for helping with the review!

@bors
Copy link
Contributor

bors commented Jan 31, 2024

📌 Commit 6619e8c has been approved by flip1995

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Jan 31, 2024

⌛ Testing commit 6619e8c with merge b58b88c...

@bors
Copy link
Contributor

bors commented Jan 31, 2024

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: flip1995
Pushing b58b88c to master...

@bors bors merged commit b58b88c into rust-lang:master Jan 31, 2024
8 checks passed
@Alexendoo Alexendoo deleted the lint-groups-priority branch February 1, 2024 13:32
jwodder added a commit to jwodder/rsrepo that referenced this pull request Feb 10, 2024
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.

5 participants