-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add suggestion to quote inlined format argument as string literal #114507
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
mod foo { | ||
pub fn bar() -> i32 { | ||
1 | ||
} | ||
} | ||
|
||
fn bar() -> i32 { | ||
2 | ||
} | ||
|
||
fn main() { | ||
let stderr = 3; | ||
eprintln!({stderr}); | ||
//~^ ERROR format argument must be a string literal | ||
//~| HELP quote your inlined format argument to use as string literal | ||
eprintln!({1}); | ||
//~^ ERROR format argument must be a string literal | ||
//~| HELP you might be missing a string literal to format with | ||
eprintln!({foo::bar()}); | ||
//~^ ERROR format argument must be a string literal | ||
//~| HELP you might be missing a string literal to format with | ||
eprintln!({bar()}); | ||
//~^ ERROR format argument must be a string literal | ||
//~| HELP you might be missing a string literal to format with | ||
eprintln!({1; 2}); | ||
//~^ ERROR format argument must be a string literal | ||
//~| HELP you might be missing a string literal to format with | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
error: format argument must be a string literal | ||
--> $DIR/suggest-inline-args.rs:13:15 | ||
| | ||
LL | eprintln!({stderr}); | ||
| ^^^^^^^^ | ||
| | ||
help: quote your inlined format argument to use as string literal | ||
| | ||
LL | eprintln!("{stderr}"); | ||
| + + | ||
|
||
error: format argument must be a string literal | ||
--> $DIR/suggest-inline-args.rs:16:15 | ||
| | ||
LL | eprintln!({1}); | ||
| ^^^ | ||
| | ||
help: you might be missing a string literal to format with | ||
| | ||
LL | eprintln!("{}", {1}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: removing the braces when there is only one distinct 'statement' would be nice, if you're up for it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for the suggestion! will look into it outside of this PR 😄 |
||
| +++++ | ||
|
||
error: format argument must be a string literal | ||
--> $DIR/suggest-inline-args.rs:19:15 | ||
| | ||
LL | eprintln!({foo::bar()}); | ||
| ^^^^^^^^^^^^ | ||
| | ||
help: you might be missing a string literal to format with | ||
| | ||
LL | eprintln!("{}", {foo::bar()}); | ||
| +++++ | ||
|
||
error: format argument must be a string literal | ||
--> $DIR/suggest-inline-args.rs:22:15 | ||
| | ||
LL | eprintln!({bar()}); | ||
| ^^^^^^^ | ||
| | ||
help: you might be missing a string literal to format with | ||
| | ||
LL | eprintln!("{}", {bar()}); | ||
| +++++ | ||
|
||
error: format argument must be a string literal | ||
--> $DIR/suggest-inline-args.rs:25:15 | ||
| | ||
LL | eprintln!({1; 2}); | ||
| ^^^^^^ | ||
| | ||
help: you might be missing a string literal to format with | ||
| | ||
LL | eprintln!("{}", {1; 2}); | ||
| +++++ | ||
|
||
error: aborting due to 5 previous errors | ||
|
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 can't think of any situations where this suggestion would ever produce code that wouldn't compile. i'm not exactly sure of the exact use cases for
Applicability::MachineApplicable
and how rigorously provable the suggestions must be, but due to the relatively simplicity of the suggestion and the general unambiguity surrounding the purpose ofprintln!
and associated macros, I think its fair to annotate this suggestion asMachineApplicable
. let me know if i'm overlooking something obvious thoughThere 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 the original
you might be missing a string literal to format with
suggestion, I can't think of cases where the suggestion would produce code that wouldn't compile too. Yet, it usesApplicability::MaybeIncorrect
. This is why I usedApplicability::MaybeIncorrect
in this new suggestion. Do you think I should change both toApplicability::MachineApplicable
?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 keeping
MaybeIncorrect
is probably best for now.