Skip to content

Fix single_char_pattern lint for escaped chars #2999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions clippy_lints/src/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1888,11 +1888,8 @@ fn lint_chars_last_cmp_with_unwrap<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, info: &
fn lint_single_char_pattern<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, _expr: &'tcx hir::Expr, arg: &'tcx hir::Expr) {
if let Some((Constant::Str(r), _)) = constant(cx, cx.tables, arg) {
if r.len() == 1 {
let c = r.chars().next().unwrap();
let snip = snippet(cx, arg.span, "..");
let hint = snip.replace(
&format!("\"{}\"", c.escape_default()),
&format!("'{}'", c.escape_default()));
let hint = format!("'{}'", &snip[1..snip.len() - 1]);
span_lint_and_sugg(
cx,
SINGLE_CHAR_PATTERN,
Expand Down
1 change: 1 addition & 0 deletions tests/ui/single_char_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ fn main() {
h.contains("X"); // should not warn

x.replace(";", ",").split(","); // issue #2978
x.starts_with("\x03"); // issue #2996
}
8 changes: 7 additions & 1 deletion tests/ui/single_char_pattern.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,11 @@ error: single-character string constant used as pattern
44 | x.replace(";", ",").split(","); // issue #2978
| ^^^ help: try using a char instead: `','`

error: aborting due to 19 previous errors
error: single-character string constant used as pattern
--> $DIR/single_char_pattern.rs:45:19
|
45 | x.starts_with("/x03"); // issue #2996
| ^^^^^^ help: try using a char instead: `'/x03'`

error: aborting due to 20 previous errors