-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Correct suggestion for println #51614
Conversation
src/test/ui/macros/bad_hello.stderr
Outdated
@@ -2,7 +2,7 @@ error: expected a literal | |||
--> $DIR/bad_hello.rs:12:14 | |||
| | |||
LL | println!(3 + 4); //~ ERROR expected a literal | |||
| ^^^^^ | |||
| ^^^^^ help: consider changing this to: `"{}", 3 + 4` |
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 this suggestion as-is could be a little confusing: selecting the entire expression and suggesting it be replaced with println!("{}", 3 + 4)
would give more helpful context.
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.
An alternative to avoid having to play with the spans can be to make the help message long enough that it no longer is suggested in place, instead showing the new code on its own:
help: you might be missing a string literal to format with
|
LL | println!("{}", 3 + 4); //~ ERROR expected a literal
| ^^^^^^^^^^^
src/libsyntax_ext/concat.rs
Outdated
err.span_suggestion( | ||
e.span, | ||
"consider changing this to", | ||
format!("\"{{}}\", {}", pprust::expr_to_string(&e)) |
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.
Could you use span to snippet here and only use expression to string only if that call fails? We want to preserve the code as is, like for example whitespace.
src/libsyntax_ext/concat.rs
Outdated
"consider changing this to", | ||
format!("\"{{}}\", {}", pprust::expr_to_string(&e)) | ||
); | ||
err.emit(); |
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.
Could you add a note explaining that a format string is always needed?
Ping form triage, @csmoe: It looks like there are some comments on your PR, do you think you'll be able to address those? |
@TimNN testing locally, more commits later. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
This comment has been minimized.
This comment has been minimized.
Ping from triage, @estebank ! This PR requires your review. |
src/libsyntax_ext/concat.rs
Outdated
@@ -53,7 +53,12 @@ pub fn expand_syntax_ext(cx: &mut base::ExtCtxt, | |||
} | |||
} | |||
_ => { | |||
cx.span_err(e.span, "expected a literal"); | |||
let mut err = cx.struct_span_err(e.span, "expected a literal"); | |||
err.span_help( |
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.
This should be
if let Some(snippet) = cx.codemap().span_to_snippet(span) {
err.span_suggestion(
e.span,
"you might be missing a string literal to format with",
format!("\"{{}}\", {}", snippet);
);
}
Which will give you the following output:
error: expected a literal
--> $DIR/bad_hello.rs:12:14
|
LL | println!(3 + 4); //~ ERROR expected a literal
| ^^^^^
|
help: you might be missing a string literal to format with
--> $DIR/bad_hello.rs:12:14
|
LL | println!("{}", 3 + 4); //~ ERROR expected a literal
| ^^^^^^^^^^^
This comment has been minimized.
This comment has been minimized.
Oh, sorry, it is a |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@bors r+ rollup |
📌 Commit 790c09e has been approved by |
Correct suggestion for println Closes rust-lang#51585 r? @estebank
Rollup of 14 pull requests Successful merges: - #51614 (Correct suggestion for println) - #51952 ( hygiene: Decouple transparencies from expansion IDs) - #52193 (step_by: leave time of item skip unspecified) - #52207 (improve error message shown for unsafe operations) - #52223 (Deny bare trait objects in in src/liballoc) - #52224 (Deny bare trait objects in in src/libsyntax) - #52239 (Remove sync::Once::call_once 'static bound) - #52247 (Deny bare trait objects in in src/librustc) - #52248 (Deny bare trait objects in in src/librustc_allocator) - #52252 (Deny bare trait objects in in src/librustc_codegen_llvm) - #52253 (Deny bare trait objects in in src/librustc_data_structures) - #52254 (Deny bare trait objects in in src/librustc_metadata) - #52261 (Deny bare trait objects in in src/libpanic_unwind) - #52265 (Deny bare trait objects in in src/librustc_codegen_utils) Failed merges: r? @ghost
Closes #51585
r? @estebank