Skip to content

Commit 431917c

Browse files
authored
Merge pull request #2737 from flip1995/escaped_squigglies
panic_params: don't lint escaped squigglies
2 parents ad438b3 + db4e7ac commit 431917c

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

clippy_lints/src/panic.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ use utils::{is_direct_expn_of, match_def_path, opt_def_id, paths, resolve_node,
1010
/// is not a format string and used literally. So while `format!("{}")` will
1111
/// fail to compile, `panic!("{}")` will not.
1212
///
13-
/// **Known problems:** Should you want to use curly brackets in `panic!`
14-
/// without any parameter, this lint will warn.
13+
/// **Known problems:** None.
1514
///
1615
/// **Example:**
1716
/// ```rust
@@ -45,8 +44,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
4544
if let ExprLit(ref lit) = params[0].node;
4645
if is_direct_expn_of(expr.span, "panic").is_some();
4746
if let LitKind::Str(ref string, _) = lit.node;
48-
if let Some(par) = string.as_str().find('{');
49-
if string.as_str()[par..].contains('}');
47+
let string = string.as_str().replace("{{", "").replace("}}", "");
48+
if let Some(par) = string.find('{');
49+
if string[par..].contains('}');
5050
if params[0].span.source_callee().is_none();
5151
if params[0].span.lo() != params[0].span.hi();
5252
then {

tests/ui/panic.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ fn missing() {
1111
} else {
1212
assert!(true, "here be missing values: {}");
1313
}
14+
15+
panic!("{{{this}}}");
1416
}
1517

1618
fn ok_single() {
@@ -41,11 +43,22 @@ fn ok_nomsg() {
4143
assert!(if 1 == ONE { ONE == 1 } else { false });
4244
}
4345

46+
fn ok_escaped() {
47+
panic!("{{ why should this not be ok? }}");
48+
panic!(" or {{ that ?");
49+
panic!(" or }} this ?");
50+
panic!(" {or {{ that ?");
51+
panic!(" }or }} this ?");
52+
panic!("{{ test }");
53+
panic!("{case }}");
54+
}
55+
4456
fn main() {
4557
missing();
4658
ok_single();
4759
ok_multiple();
4860
ok_bracket();
4961
ok_inner();
5062
ok_nomsg();
63+
ok_escaped();
5164
}

tests/ui/panic.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,11 @@ error: you probably are missing some parameter in your format string
1818
12 | assert!(true, "here be missing values: {}");
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2020

21-
error: aborting due to 3 previous errors
21+
error: you probably are missing some parameter in your format string
22+
--> $DIR/panic.rs:15:12
23+
|
24+
15 | panic!("{{{this}}}");
25+
| ^^^^^^^^^^^^
26+
27+
error: aborting due to 4 previous errors
2228

0 commit comments

Comments
 (0)