Skip to content

Commit 790c09e

Browse files
committed
suggest on new snippet
1 parent 88f475c commit 790c09e

File tree

2 files changed

+31
-33
lines changed

2 files changed

+31
-33
lines changed

src/libsyntax_ext/concat.rs

+29-29
Original file line numberDiff line numberDiff line change
@@ -12,51 +12,51 @@ 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
_ => {
5654
let mut err = cx.struct_span_err(e.span, "expected a literal");
57-
err.span_help(
55+
let snippet = cx.codemap().span_to_snippet(e.span).unwrap();
56+
err.span_suggestion(
5857
e.span,
5958
"you might be missing a string literal to format with",
59+
format!("\"{{}}\", {}", snippet),
6060
);
6161
err.emit();
6262
}

src/test/ui/macros/bad_hello.stderr

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ error: expected a literal
33
|
44
LL | println!(3 + 4); //~ ERROR expected a literal
55
| ^^^^^
6-
|
76
help: you might be missing a string literal to format with
8-
--> $DIR/bad_hello.rs:12:14
97
|
10-
LL | println!(3 + 4); //~ ERROR expected a literal
11-
| ^^^^^
8+
LL | println!("{}", 3 + 4); //~ ERROR expected a literal
9+
| ^^^^^^^^^^^
1210

1311
error: aborting due to previous error
1412

0 commit comments

Comments
 (0)