Skip to content

Commit d2a8a2b

Browse files
Rollup merge of rust-lang#51614 - csmoe:lit_sugg, r=estebank
Correct suggestion for println Closes rust-lang#51585 r? @estebank
2 parents d573fe1 + 790c09e commit d2a8a2b

File tree

2 files changed

+38
-29
lines changed

2 files changed

+38
-29
lines changed

src/libsyntax_ext/concat.rs

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,53 @@ use syntax::ast;
1212
use syntax::ext::base;
1313
use syntax::ext::build::AstBuilder;
1414
use syntax::symbol::Symbol;
15-
use syntax_pos;
1615
use syntax::tokenstream;
16+
use syntax_pos;
1717

1818
use std::string::String;
1919

20-
pub fn expand_syntax_ext(cx: &mut base::ExtCtxt,
21-
sp: syntax_pos::Span,
22-
tts: &[tokenstream::TokenTree])
23-
-> Box<base::MacResult + 'static> {
20+
pub fn expand_syntax_ext(
21+
cx: &mut base::ExtCtxt,
22+
sp: syntax_pos::Span,
23+
tts: &[tokenstream::TokenTree],
24+
) -> Box<base::MacResult + 'static> {
2425
let es = match base::get_exprs_from_tts(cx, sp, tts) {
2526
Some(e) => e,
2627
None => return base::DummyResult::expr(sp),
2728
};
2829
let mut accumulator = String::new();
2930
for e in es {
3031
match e.node {
31-
ast::ExprKind::Lit(ref lit) => {
32-
match lit.node {
33-
ast::LitKind::Str(ref s, _) |
34-
ast::LitKind::Float(ref s, _) |
35-
ast::LitKind::FloatUnsuffixed(ref s) => {
36-
accumulator.push_str(&s.as_str());
37-
}
38-
ast::LitKind::Char(c) => {
39-
accumulator.push(c);
40-
}
41-
ast::LitKind::Int(i, ast::LitIntType::Unsigned(_)) |
42-
ast::LitKind::Int(i, ast::LitIntType::Signed(_)) |
43-
ast::LitKind::Int(i, ast::LitIntType::Unsuffixed) => {
44-
accumulator.push_str(&format!("{}", i));
45-
}
46-
ast::LitKind::Bool(b) => {
47-
accumulator.push_str(&format!("{}", b));
48-
}
49-
ast::LitKind::Byte(..) |
50-
ast::LitKind::ByteStr(..) => {
51-
cx.span_err(e.span, "cannot concatenate a byte string literal");
52-
}
32+
ast::ExprKind::Lit(ref lit) => match lit.node {
33+
ast::LitKind::Str(ref s, _)
34+
| ast::LitKind::Float(ref s, _)
35+
| ast::LitKind::FloatUnsuffixed(ref s) => {
36+
accumulator.push_str(&s.as_str());
5337
}
54-
}
38+
ast::LitKind::Char(c) => {
39+
accumulator.push(c);
40+
}
41+
ast::LitKind::Int(i, ast::LitIntType::Unsigned(_))
42+
| ast::LitKind::Int(i, ast::LitIntType::Signed(_))
43+
| ast::LitKind::Int(i, ast::LitIntType::Unsuffixed) => {
44+
accumulator.push_str(&format!("{}", i));
45+
}
46+
ast::LitKind::Bool(b) => {
47+
accumulator.push_str(&format!("{}", b));
48+
}
49+
ast::LitKind::Byte(..) | ast::LitKind::ByteStr(..) => {
50+
cx.span_err(e.span, "cannot concatenate a byte string literal");
51+
}
52+
},
5553
_ => {
56-
cx.span_err(e.span, "expected a literal");
54+
let mut err = cx.struct_span_err(e.span, "expected a literal");
55+
let snippet = cx.codemap().span_to_snippet(e.span).unwrap();
56+
err.span_suggestion(
57+
e.span,
58+
"you might be missing a string literal to format with",
59+
format!("\"{{}}\", {}", snippet),
60+
);
61+
err.emit();
5762
}
5863
}
5964
}

src/test/ui/macros/bad_hello.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ error: expected a literal
33
|
44
LL | println!(3 + 4); //~ ERROR expected a literal
55
| ^^^^^
6+
help: you might be missing a string literal to format with
7+
|
8+
LL | println!("{}", 3 + 4); //~ ERROR expected a literal
9+
| ^^^^^^^^^^^
610

711
error: aborting due to previous error
812

0 commit comments

Comments
 (0)