Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Validate paths in disallowed_* configurations #14397

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

Merged
merged 1 commit into from
Apr 1, 2025

Conversation

smoelius
Copy link
Contributor

@smoelius smoelius commented Mar 12, 2025

This PR resolves #11432 by checking that paths resolve in disallowed_* configurations.

It also does some lightweight validation of definition kinds. For example, only paths that resolve to DefKind::Macro definitions are allowed in disallowed_macro configurations.

The PR is currently two commits. The first is #14376 (cc: @Jarcho), which I submitted just a few days ago. The second commit is everything else.

Side note: For me, the most difficult part of this PR was getting the spans of the individual DisallowedPath configurations. There is some discussion at toml-rs/toml#840, if interested.

changelog: validate paths in disallowed_* configurations

@rustbot
Copy link
Collaborator

rustbot commented Mar 12, 2025

r? @Manishearth

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

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Mar 12, 2025
@Manishearth
Copy link
Member

r? clippy

rather busy today, may not get to this soon

@smoelius
Copy link
Contributor Author

smoelius commented Mar 31, 2025

Rebased and updated the description now that #14376 has been merged.

@flip1995
Copy link
Member

r? @samueltardieu

@rustbot rustbot assigned samueltardieu and unassigned flip1995 Mar 31, 2025
Copy link
Contributor

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

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

This was very interesting to review. I have a few remarks and nits.

Also, could you remove the + use<'_, REPLACEMENT_ALLOWED> in types.rs:77 while you're editing the file? It should not be needed at all with Rust 2024 RPIT capture rules, both self's lifetime and REPLACEMENT_ALLOWED should be captured by default.

.dcx()
.span_warn(span, format!("primitive types are not allowed: {path}"));
} else if !disallowed_path.allow_invalid {
tcx.sess.dcx().span_warn(span, format!("`{path}` is invalid"));
Copy link
Contributor

Choose a reason for hiding this comment

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

"is invalid" seems vague. Should it be something along the line of "does not designate an existing item"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed "is invalid" to "may not refer to an existing definition" everywhere.

I went with "may not" instead of "does not" because the existence depends on the compiler used. For example, the rustc_private paths in Clippy's clippy.toml will exist only if a nightly compiler is used.

I went with "definitions" instead of "items" mainly because it sounds more natural to me. But also this list from the Rust Reference suggests "definitions" is more specific than "items", and I think it applies to all of our cases.

I'm not wed to either and I'll change them if you feel strongly.

Copy link
Contributor

Choose a reason for hiding this comment

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

I like the change in the message shown to the user. However, in the configuration file, I find it quite lengthy. Maybe there allow-invalid would be fine, as long as the config option doc explains what it does. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good to me.

FxHashMap::default();
for disallowed_path in disallowed_paths {
let path = disallowed_path.path();
let mut reses = clippy_utils::def_path_res(tcx, &path.split("::").collect::<Vec<_>>());
Copy link
Contributor

Choose a reason for hiding this comment

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

resolutions might be an easier name to read and understand.

{
(id, expr.span)
},
// Previous versions of this lint checked the `DefKind` in the next arm. However, that is no longer
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: I don't think it is appropriate to talk about "previous versions" in comments. Explanations of change should be in the commit message, while explanation about the current code (if needed) should be in comments.

if found_def_id {
tcx.sess
.dcx()
.span_warn(span, format!("`{path}` is not of an appropriate kind"));
Copy link
Contributor

@samueltardieu samueltardieu Mar 31, 2025

Choose a reason for hiding this comment

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

You can give a better message by, for example:

  • giving this function an extra &str parameter predicate_description (here: "a type")
  • storing an Option<DefId> in found_def_id instead of a bool
  • using tcx.def_descr_article() and tcx.def_descr()
    to generate a warning message such as:
warning: `std::result::Result::Err`: expected a type, found a tuple variant
  --> /home/sam/Dev/rust-clippy/tests/ui-toml/toml_invalid_path/clippy.toml:1:1
   |
LL | / [[disallowed-types]]
LL | | path = "std::result::Result::Err"
   | |_________________________________^

@@ -517,9 +572,11 @@ define_Conf! {
#[conf_deprecated("Please use `cognitive-complexity-threshold` instead", cognitive_complexity_threshold)]
cyclomatic_complexity_threshold: u64 = 25,
/// The list of disallowed macros, written as fully qualified paths.
#[disallowed_path_replacements_allowed = true]
Copy link
Contributor

Choose a reason for hiding this comment

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

This is really hard to parse. I had to think twice to distinguish between disallowed(path_replacements_allowed) and disallowed_path_replacements: allowed.

What about something like allow_path_replacements_suggestions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Your point is very well taken. But, for context, disallowed_path_replacements_allowed is the concatenation of DisallowedPath and the REPLACEMENTS_ALLOWED const parameter, made lower-snake-case.

That attribute is meant to serve as both a declaration (i.e., "disallowed paths are used in this configuration") and the setting of the REPLACEMENTS_ALLOWED parameter.

I'm struggling with allow_path_replacements_suggestions because, to me, it does not sound like a declaration.

I changed all of the attributes to disallowed_path_allow_replacements. Is that better?

(value, value_span)
}};

($map:expr, $ty:ty, $errors:expr, $file:expr, $replacements_allowed:expr) => {{
Copy link
Contributor

Choose a reason for hiding this comment

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

I would have found it clearer if you add started the pattern with something like (disallowed_paths => …), to clearly show that this is something used by this lint family, instead of relying on matching the extra argument.

Copy link
Contributor Author

@smoelius smoelius Apr 1, 2025

Choose a reason for hiding this comment

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

I'm trying to apply a "trick" that I learned from the default_text macro defined in the same file.

Specifically, on this line, the replacements_allowed argument is included only if the disallowed_path_replacements_allowed attribute was used:

deserialize!(map, $ty, errors, self.0 $(, $replacements_allowed)?);

In this way, the presence/absence of that argument selects between the two arms in the deserialize! macro.

Your suggestion is to use a keyword to choose between the two arms, correct?

I take your point that the present code is subtle and non-obvious. But, to be honest, I cannot immediately see how to make the keyword approach work. Could you offer me some more guidance? Or is there a way to make the present code more clear?

(Sorry if the "trick" explanation was unnecessary.)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I've noticed that (that was my "instead of relying on matching the extra argument" point). The idea with the keyword would be to make it clearer that this specific branch will be used by the disallowed_* lints.

But since you get the replacements_allowed from the defineConf macro, it is right that it would be more complicated to use such a keyword. You can forget about the suggestion.

@samueltardieu
Copy link
Contributor

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Apr 1, 2025
@samueltardieu samueltardieu added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Apr 1, 2025
@samueltardieu
Copy link
Contributor

Btw @smoelius, you'll need to rebase on master after #14514 has been merged, so that lintcheck can run.

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Apr 1, 2025
@rustbot

This comment has been minimized.

@rustbot rustbot removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Apr 1, 2025
Copy link
Contributor

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

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

Thanks for your quick reaction to the feedback. This looks good to me, and a real improvement.

Could you please squash the commits into one? I'll then merge it.

@samueltardieu
Copy link
Contributor

Just a nitpick: it looks like you kept "Addressed review comments", as well as the parameter change in the commit message when squashing. Can you just edit it to its purpose, i.e. "Validate path in …"?

@samueltardieu samueltardieu added this pull request to the merge queue Apr 1, 2025
Merged via the queue into rust-lang:master with commit e975563 Apr 1, 2025
11 checks passed
@smoelius
Copy link
Contributor Author

smoelius commented Apr 1, 2025

Thanks!

@smoelius smoelius deleted the validate-paths branch April 1, 2025 19:19
ojeda added a commit to ojeda/linux that referenced this pull request May 2, 2025
…onfiguration

Starting with Rust 1.88.0 (expected 2025-06-26) [1], Clippy may start
warning about paths that do not resolve in the `disallowed_macros`
configuration:

    warning: `kernel::dbg` does not refer to an existing macro
      --> .clippy.toml:10:5
       |
    10 |     { path = "kernel::dbg", reason = "the `dbg!` macro is intended as a debugging tool" },
       |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This is a lint we requested at [2], due to the trouble debugging
the lint due to false negatives (e.g. [3]), which we use to emulate
`clippy::dbg_macro` [4]. See commit 8577c9d ("rust: replace
`clippy::dbg_macro` with `disallowed_macros`") for more details.

Given the false negatives are not resolved yet, it is expected that
Clippy complains about not finding this macro.

Thus, until the false negatives are fixed (and, even then, probably we
will need to wait for the MSRV to raise enough), use the escape hatch
to allow an invalid path.

Cc: [email protected] # Needed in 6.12.y and later (Rust is pinned in older LTSs).
Link: rust-lang/rust-clippy#14397 [1]
Link: rust-lang/rust-clippy#11432 [2]
Link: rust-lang/rust-clippy#11431 [3]
Link: rust-lang/rust-clippy#11303 [4]
Signed-off-by: Miguel Ojeda <[email protected]>
ojeda added a commit to ojeda/linux that referenced this pull request May 2, 2025
…onfiguration

Starting with Rust 1.88.0 (expected 2025-06-26) [1], Clippy may start
warning about paths that do not resolve in the `disallowed_macros`
configuration:

    warning: `kernel::dbg` does not refer to an existing macro
      --> .clippy.toml:10:5
       |
    10 |     { path = "kernel::dbg", reason = "the `dbg!` macro is intended as a debugging tool" },
       |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This is a lint we requested at [2], due to the trouble debugging
the lint due to false negatives (e.g. [3]), which we use to emulate
`clippy::dbg_macro` [4]. See commit 8577c9d ("rust: replace
`clippy::dbg_macro` with `disallowed_macros`") for more details.

Given the false negatives are not resolved yet, it is expected that
Clippy complains about not finding this macro.

Thus, until the false negatives are fixed (and, even then, probably we
will need to wait for the MSRV to raise enough), use the escape hatch
to allow an invalid path.

Cc: [email protected] # Needed in 6.12.y and later (Rust is pinned in older LTSs).
Link: rust-lang/rust-clippy#14397 [1]
Link: rust-lang/rust-clippy#11432 [2]
Link: rust-lang/rust-clippy#11431 [3]
Link: rust-lang/rust-clippy#11303 [4]
Signed-off-by: Miguel Ojeda <[email protected]>
ojeda added a commit to ojeda/linux that referenced this pull request May 2, 2025
…onfiguration

Starting with Rust 1.88.0 (expected 2025-06-26) [1], Clippy may start
warning about paths that do not resolve in the `disallowed_macros`
configuration:

    warning: `kernel::dbg` does not refer to an existing macro
      --> .clippy.toml:10:5
       |
    10 |     { path = "kernel::dbg", reason = "the `dbg!` macro is intended as a debugging tool" },
       |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This is a lint we requested at [2], due to the trouble debugging
the lint due to false negatives (e.g. [3]), which we use to emulate
`clippy::dbg_macro` [4]. See commit 8577c9d ("rust: replace
`clippy::dbg_macro` with `disallowed_macros`") for more details.

Given the false negatives are not resolved yet, it is expected that
Clippy complains about not finding this macro.

Thus, until the false negatives are fixed (and, even then, probably we
will need to wait for the MSRV to raise enough), use the escape hatch
to allow an invalid path.

Cc: [email protected] # Needed in 6.12.y and later (Rust is pinned in older LTSs).
Link: rust-lang/rust-clippy#14397 [1]
Link: rust-lang/rust-clippy#11432 [2]
Link: rust-lang/rust-clippy#11431 [3]
Link: rust-lang/rust-clippy#11303 [4]
Signed-off-by: Miguel Ojeda <[email protected]>
intel-lab-lkp pushed a commit to intel-lab-lkp/linux that referenced this pull request May 2, 2025
…onfiguration

Starting with Rust 1.88.0 (expected 2025-06-26) [1], Clippy may start
warning about paths that do not resolve in the `disallowed_macros`
configuration:

    warning: `kernel::dbg` does not refer to an existing macro
      --> .clippy.toml:10:5
       |
    10 |     { path = "kernel::dbg", reason = "the `dbg!` macro is intended as a debugging tool" },
       |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This is a lint we requested at [2], due to the trouble debugging
the lint due to false negatives (e.g. [3]), which we use to emulate
`clippy::dbg_macro` [4]. See commit 8577c9d ("rust: replace
`clippy::dbg_macro` with `disallowed_macros`") for more details.

Given the false negatives are not resolved yet, it is expected that
Clippy complains about not finding this macro.

Thus, until the false negatives are fixed (and, even then, probably we
will need to wait for the MSRV to raise enough), use the escape hatch
to allow an invalid path.

Cc: [email protected] # Needed in 6.12.y and later (Rust is pinned in older LTSs).
Link: rust-lang/rust-clippy#14397 [1]
Link: rust-lang/rust-clippy#11432 [2]
Link: rust-lang/rust-clippy#11431 [3]
Link: rust-lang/rust-clippy#11303 [4]
Signed-off-by: Miguel Ojeda <[email protected]>
@RGBCube
Copy link

RGBCube commented May 3, 2025

image

This seems to have false-positives if I'm using the lint correctly, I'm on clippy 0.1.88 (3350c1eb3f 2025-05-01).

@smoelius
Copy link
Contributor Author

smoelius commented May 3, 2025

@RGBCube Can you share the code used to produce that screenshot?

@RGBCube
Copy link

RGBCube commented May 3, 2025

@RGBCube Can you share the code used to produce that screenshot?

It's at https://github.com/cull-os/carcass/blob/d3db2eb953acbbce7fb1420c92f2103be5a7d7ff/cab/cab-format/src/width.rs#L39, trying to get a MRE right now.

@RGBCube
Copy link

RGBCube commented May 3, 2025

Found the problem, it checks against crate dependencies, instead of scanning the workspace lock. This happens when you:

  • Have package foo in a workspace which depends on unicode_width
  • Have package bar in the same workspace that doesn't depend on unicode_width
  • Clippy check package bar: cargo clippy --package bar

You of course get the error when you run clippy normally without --package, as it checks every one individually.

I think checking the top-level Cargo.lock would fix this. Though I am not sure how you would handle aliases properly (like if foo did unic_width = { package = "unicode-width", ... } and clippy.toml banned unic_width::*)

Or maybe aggregate the dependencies when checking multiple packages at once, to check against?

smoelius added a commit to smoelius/rust-clippy that referenced this pull request May 4, 2025
@smoelius
Copy link
Contributor Author

smoelius commented May 4, 2025

@RGBCube Thanks for the detailed analysis, which made the issue very easy to reproduce. 🙏

github-merge-queue bot pushed a commit that referenced this pull request May 4, 2025
Fixes
#14397 (comment)

r? @samueltardieu

changelog: Don't warn about clippy.toml disallowed paths for crates that
were not loaded
ojeda added a commit to Rust-for-Linux/linux that referenced this pull request May 6, 2025
…onfiguration

Starting with Rust 1.88.0 (expected 2025-06-26) [1], Clippy may start
warning about paths that do not resolve in the `disallowed_macros`
configuration:

    warning: `kernel::dbg` does not refer to an existing macro
      --> .clippy.toml:10:5
       |
    10 |     { path = "kernel::dbg", reason = "the `dbg!` macro is intended as a debugging tool" },
       |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This is a lint we requested at [2], due to the trouble debugging
the lint due to false negatives (e.g. [3]), which we use to emulate
`clippy::dbg_macro` [4]. See commit 8577c9d ("rust: replace
`clippy::dbg_macro` with `disallowed_macros`") for more details.

Given the false negatives are not resolved yet, it is expected that
Clippy complains about not finding this macro.

Thus, until the false negatives are fixed (and, even then, probably we
will need to wait for the MSRV to raise enough), use the escape hatch
to allow an invalid path.

Cc: [email protected] # Needed in 6.12.y and later (Rust is pinned in older LTSs).
Link: rust-lang/rust-clippy#14397 [1]
Link: rust-lang/rust-clippy#11432 [2]
Link: rust-lang/rust-clippy#11431 [3]
Link: rust-lang/rust-clippy#11303 [4]
Reviewed-by: Alice Ryhl <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Miguel Ojeda <[email protected]>
ojeda added a commit to Rust-for-Linux/linux that referenced this pull request May 6, 2025
…onfiguration

Starting with Rust 1.88.0 (expected 2025-06-26) [1], Clippy may start
warning about paths that do not resolve in the `disallowed_macros`
configuration:

    warning: `kernel::dbg` does not refer to an existing macro
      --> .clippy.toml:10:5
       |
    10 |     { path = "kernel::dbg", reason = "the `dbg!` macro is intended as a debugging tool" },
       |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This is a lint we requested at [2], due to the trouble debugging
the lint due to false negatives (e.g. [3]), which we use to emulate
`clippy::dbg_macro` [4]. See commit 8577c9d ("rust: replace
`clippy::dbg_macro` with `disallowed_macros`") for more details.

Given the false negatives are not resolved yet, it is expected that
Clippy complains about not finding this macro.

Thus, until the false negatives are fixed (and, even then, probably we
will need to wait for the MSRV to raise enough), use the escape hatch
to allow an invalid path.

Cc: [email protected] # Needed in 6.12.y and later (Rust is pinned in older LTSs).
Link: rust-lang/rust-clippy#14397 [1]
Link: rust-lang/rust-clippy#11432 [2]
Link: rust-lang/rust-clippy#11431 [3]
Link: rust-lang/rust-clippy#11303 [4]
Reviewed-by: Alice Ryhl <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Miguel Ojeda <[email protected]>
ojeda added a commit to Rust-for-Linux/linux that referenced this pull request May 6, 2025
…onfiguration

Starting with Rust 1.88.0 (expected 2025-06-26) [1], Clippy may start
warning about paths that do not resolve in the `disallowed_macros`
configuration:

    warning: `kernel::dbg` does not refer to an existing macro
      --> .clippy.toml:10:5
       |
    10 |     { path = "kernel::dbg", reason = "the `dbg!` macro is intended as a debugging tool" },
       |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This is a lint we requested at [2], due to the trouble debugging
the lint due to false negatives (e.g. [3]), which we use to emulate
`clippy::dbg_macro` [4]. See commit 8577c9d ("rust: replace
`clippy::dbg_macro` with `disallowed_macros`") for more details.

Given the false negatives are not resolved yet, it is expected that
Clippy complains about not finding this macro.

Thus, until the false negatives are fixed (and, even then, probably we
will need to wait for the MSRV to raise enough), use the escape hatch
to allow an invalid path.

Cc: [email protected] # Needed in 6.12.y and later (Rust is pinned in older LTSs).
Link: rust-lang/rust-clippy#14397 [1]
Link: rust-lang/rust-clippy#11432 [2]
Link: rust-lang/rust-clippy#11431 [3]
Link: rust-lang/rust-clippy#11303 [4]
Reviewed-by: Alice Ryhl <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Miguel Ojeda <[email protected]>
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.

Diagnostic for unexpected paths in configuration file
6 participants