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

Fix literal_string_with_formatting_args lint emitted when it should not #13953

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

GuillaumeGomez
Copy link
Member

@GuillaumeGomez GuillaumeGomez commented Jan 6, 2025

Fixes #13885.
Fixes #14007.

Problem was that I forgot to check whether or not the span was a "real" one. Because if not, then it starts pointing to pretty much only wrong content, hence the problems we saw with clippy linting on clippy.toml.

changelog: Fix literal_string_with_formatting_args lint emitted when it should not

r? @samueltardieu

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.

LGTM, with the addition of a non-formatting true positive macro test case.

You should reroll the dice as I don't have the privilege to approve this patch.

r?

);
let value = 0;
assert!(format!("{value}").is_ascii());
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you also add another true positive test such as

Suggested change
}
assert!("{value}".is_ascii());
}

so that we can check that the lint properly triggers in non-formatting-macro arguments?

@rustbot
Copy link
Collaborator

rustbot commented Jan 7, 2025

Error: Parsing assign command in comment failed: ...'' | error: specify user to assign to at >| ''...

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.

1 similar comment
@rustbot
Copy link
Collaborator

rustbot commented Jan 7, 2025

Error: Parsing assign command in comment failed: ...'' | error: specify user to assign to at >| ''...

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.

@samueltardieu
Copy link
Contributor

r? clippy

@rustbot rustbot assigned xFrednet and unassigned samueltardieu Jan 7, 2025
@profetia
Copy link
Contributor

profetia commented Jan 7, 2025

Can you check if this case works? I think just checking dummy is not enough, that is why I switched to checking the enclosing exprs in #13898

@samueltardieu
Copy link
Contributor

Can you check if this case works? I think just checking dummy is not enough, that is why I switched to checking the enclosing exprs in #13898

It still triggers for me indeed:

warning: this looks like a formatting argument but it is not part of a formatting macro
 --> {value}.rs:7:6
  |
7 |     dbg!("something");
  |      ^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#literal_string_with_formatting_args
  = note: `#[warn(clippy::literal_string_with_formatting_args)]` on by default

@GuillaumeGomez GuillaumeGomez force-pushed the fix-literal_string_with_formatting_args branch from 9515896 to a7fb37c Compare January 7, 2025 14:55
@GuillaumeGomez
Copy link
Member Author

GuillaumeGomez commented Jan 7, 2025

Indeed. Sadly, @profetia, your fix doesn't lint with:

assert!("{y}".is_ascii());

So I have a not so great middle-ground solution but I think it'll do the trick for the time being: I simply check if the string is part of the snippet in the user code. If not, then I skip the lint. What do you think?

@y21
Copy link
Member

y21 commented Jan 10, 2025

Nominating for beta backport because the lint is on beta now and the false positive leads to pretty confusing warnings.

@rustbot label +beta-nominated

@rustbot rustbot added the beta-nominated Nominated for backporting to the compiler in the beta channel. label Jan 10, 2025
@GuillaumeGomez
Copy link
Member Author

Changelog CI also passed. :)

@Icxolu Icxolu mentioned this pull request Jan 11, 2025
@GuillaumeGomez
Copy link
Member Author

r? @y21

@rustbot rustbot assigned y21 and unassigned xFrednet Jan 14, 2025

fn another_bad() {
let literal_string_with_formatting_args = 0;
dbg!("something");
Copy link
Member

Choose a reason for hiding this comment

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

I'm surprised that the expr.span.from_expansion() check doesn't already prevent this. Looking at -Zunpretty=hir-tree output it does seem like the span of the string with the file name is properly marked as being from the non-root context

Lit(
    Spanned {
        node: Str(
            "[/app/example.rs:2:5] \"something\" = ",
            Cooked,
        ),
        span: /rustc/eb54a50837ad4bcc9842924f27e7287ca66e294c/library/std/src/macros.rs:365:35: 365:58 (#4),
    },
),

Odd

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I see what's happening. Clippy sets -Zflatten_format_args=no, which changes the default behavior of how format args are lowered and only that one has this non-macro span. Adding the flag makes it repro on godbolt and shows a root context span for the file name string.

Lit(
    Spanned {
        node: Str(
            "/app/example.rs",
            Cooked,
        ),
        span: /app/example.rs:2:5: 2:22 (#0),
    },
)

Maybe add to the comment that clippy sets this flag and only with that flag does it have a non-macro span which gets around the from_expansion check, in case anyone wants to look into this in the future and doesn't have to go down this rabbit hole?

Copy link
Member Author

Choose a reason for hiding this comment

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

I looked into it but couldn't figure out why it didn't work. Thanks a lot for the detailed explanations! Adding it as a comment.

Comment on lines 99 to 107
let Some(snippet) = snippet_opt(cx, expr.span) else {
return;
};
let fmt_str = symbol.as_str();
// If the literal has been generated by the macro, the snippet should not contain it,
// allowing us to skip it.
if !snippet.contains(fmt_str) {
return;
}
Copy link
Member

Choose a reason for hiding this comment

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

This will add false negatives for escaped strings (eg "{foo} \n bar") and similar, but as a temporary solution to fix FPs it's probably fine

Copy link
Member

Choose a reason for hiding this comment

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

It might be worth trying to use is_from_proc_macro here instead, because that also solves the same issue of a node not matching up with its snippet. It seems like it should also fix the FP while not running into this FN since it doesn't actually check the string contents.

(If we do go with the current approach, a small nit: it might be a good use for the newer SpanRange API with if !expr.span.check_source_text(|snippet| snippet.contains(fmt_str)) { return; }. Slightly simpler and avoids allocating a temporary string)

Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure why is_from_proc_macro didn't work last time I tried but this time it behaves as expected, so switching back to it.

@GuillaumeGomez
Copy link
Member Author

@rustbot ready

@samueltardieu
Copy link
Contributor

As it is currently, this does not fix #14007.

@GuillaumeGomez
Copy link
Member Author

Fixed #14007 as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
beta-nominated Nominated for backporting to the compiler in the beta channel. 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.

A Compiler bug literal_string_with_formatting_args false positive on test
6 participants