panic_params: don't lint escaped squigglies

This commit is contained in:
flip1995 2018-05-06 17:26:47 +02:00
parent ad438b30de
commit db4e7ac725
No known key found for this signature in database
GPG key ID: 6757AB26F72F0084
3 changed files with 24 additions and 5 deletions

View file

@ -10,8 +10,7 @@ use utils::{is_direct_expn_of, match_def_path, opt_def_id, paths, resolve_node,
/// is not a format string and used literally. So while `format!("{}")` will /// is not a format string and used literally. So while `format!("{}")` will
/// fail to compile, `panic!("{}")` will not. /// fail to compile, `panic!("{}")` will not.
/// ///
/// **Known problems:** Should you want to use curly brackets in `panic!` /// **Known problems:** None.
/// without any parameter, this lint will warn.
/// ///
/// **Example:** /// **Example:**
/// ```rust /// ```rust
@ -45,8 +44,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
if let ExprLit(ref lit) = params[0].node; if let ExprLit(ref lit) = params[0].node;
if is_direct_expn_of(expr.span, "panic").is_some(); if is_direct_expn_of(expr.span, "panic").is_some();
if let LitKind::Str(ref string, _) = lit.node; if let LitKind::Str(ref string, _) = lit.node;
if let Some(par) = string.as_str().find('{'); let string = string.as_str().replace("{{", "").replace("}}", "");
if string.as_str()[par..].contains('}'); if let Some(par) = string.find('{');
if string[par..].contains('}');
if params[0].span.source_callee().is_none(); if params[0].span.source_callee().is_none();
if params[0].span.lo() != params[0].span.hi(); if params[0].span.lo() != params[0].span.hi();
then { then {

View file

@ -11,6 +11,8 @@ fn missing() {
} else { } else {
assert!(true, "here be missing values: {}"); assert!(true, "here be missing values: {}");
} }
panic!("{{{this}}}");
} }
fn ok_single() { fn ok_single() {
@ -41,6 +43,16 @@ fn ok_nomsg() {
assert!(if 1 == ONE { ONE == 1 } else { false }); assert!(if 1 == ONE { ONE == 1 } else { false });
} }
fn ok_escaped() {
panic!("{{ why should this not be ok? }}");
panic!(" or {{ that ?");
panic!(" or }} this ?");
panic!(" {or {{ that ?");
panic!(" }or }} this ?");
panic!("{{ test }");
panic!("{case }}");
}
fn main() { fn main() {
missing(); missing();
ok_single(); ok_single();
@ -48,4 +60,5 @@ fn main() {
ok_bracket(); ok_bracket();
ok_inner(); ok_inner();
ok_nomsg(); ok_nomsg();
ok_escaped();
} }

View file

@ -18,5 +18,11 @@ error: you probably are missing some parameter in your format string
12 | assert!(true, "here be missing values: {}"); 12 | assert!(true, "here be missing values: {}");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 3 previous errors error: you probably are missing some parameter in your format string
--> $DIR/panic.rs:15:12
|
15 | panic!("{{{this}}}");
| ^^^^^^^^^^^^
error: aborting due to 4 previous errors