Skip to content

Commit

Permalink
Rollup merge of rust-lang#51614 - csmoe:lit_sugg, r=estebank
Browse files Browse the repository at this point in the history
Correct suggestion for println

Closes rust-lang#51585
r? @estebank
  • Loading branch information
Mark-Simulacrum authored Jul 11, 2018
2 parents d573fe1 + 790c09e commit d2a8a2b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 29 deletions.
63 changes: 34 additions & 29 deletions src/libsyntax_ext/concat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,53 @@ use syntax::ast;
use syntax::ext::base;
use syntax::ext::build::AstBuilder;
use syntax::symbol::Symbol;
use syntax_pos;
use syntax::tokenstream;
use syntax_pos;

use std::string::String;

pub fn expand_syntax_ext(cx: &mut base::ExtCtxt,
sp: syntax_pos::Span,
tts: &[tokenstream::TokenTree])
-> Box<base::MacResult + 'static> {
pub fn expand_syntax_ext(
cx: &mut base::ExtCtxt,
sp: syntax_pos::Span,
tts: &[tokenstream::TokenTree],
) -> Box<base::MacResult + 'static> {
let es = match base::get_exprs_from_tts(cx, sp, tts) {
Some(e) => e,
None => return base::DummyResult::expr(sp),
};
let mut accumulator = String::new();
for e in es {
match e.node {
ast::ExprKind::Lit(ref lit) => {
match lit.node {
ast::LitKind::Str(ref s, _) |
ast::LitKind::Float(ref s, _) |
ast::LitKind::FloatUnsuffixed(ref s) => {
accumulator.push_str(&s.as_str());
}
ast::LitKind::Char(c) => {
accumulator.push(c);
}
ast::LitKind::Int(i, ast::LitIntType::Unsigned(_)) |
ast::LitKind::Int(i, ast::LitIntType::Signed(_)) |
ast::LitKind::Int(i, ast::LitIntType::Unsuffixed) => {
accumulator.push_str(&format!("{}", i));
}
ast::LitKind::Bool(b) => {
accumulator.push_str(&format!("{}", b));
}
ast::LitKind::Byte(..) |
ast::LitKind::ByteStr(..) => {
cx.span_err(e.span, "cannot concatenate a byte string literal");
}
ast::ExprKind::Lit(ref lit) => match lit.node {
ast::LitKind::Str(ref s, _)
| ast::LitKind::Float(ref s, _)
| ast::LitKind::FloatUnsuffixed(ref s) => {
accumulator.push_str(&s.as_str());
}
}
ast::LitKind::Char(c) => {
accumulator.push(c);
}
ast::LitKind::Int(i, ast::LitIntType::Unsigned(_))
| ast::LitKind::Int(i, ast::LitIntType::Signed(_))
| ast::LitKind::Int(i, ast::LitIntType::Unsuffixed) => {
accumulator.push_str(&format!("{}", i));
}
ast::LitKind::Bool(b) => {
accumulator.push_str(&format!("{}", b));
}
ast::LitKind::Byte(..) | ast::LitKind::ByteStr(..) => {
cx.span_err(e.span, "cannot concatenate a byte string literal");
}
},
_ => {
cx.span_err(e.span, "expected a literal");
let mut err = cx.struct_span_err(e.span, "expected a literal");
let snippet = cx.codemap().span_to_snippet(e.span).unwrap();
err.span_suggestion(
e.span,
"you might be missing a string literal to format with",
format!("\"{{}}\", {}", snippet),
);
err.emit();
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/macros/bad_hello.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ error: expected a literal
|
LL | println!(3 + 4); //~ ERROR expected a literal
| ^^^^^
help: you might be missing a string literal to format with
|
LL | println!("{}", 3 + 4); //~ ERROR expected a literal
| ^^^^^^^^^^^

error: aborting due to previous error

0 comments on commit d2a8a2b

Please sign in to comment.