Skip to content

Lint single-character strings as P: Pattern args #669

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 4 commits into from
Feb 16, 2016

Conversation

shssoichiro
Copy link
Contributor

Fixes #650

let hint = snippet(cx, expr.span, "..").replace(&format!("\"{}\"", r), &format!("'{}'", r));
span_lint_and_then(cx,
SINGLE_CHAR_PATTERN,
expr.span,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That one should be arg.span.

@shssoichiro
Copy link
Contributor Author

New commit pushed with @mcarton's nits fixed.

char is faster, proven by benchmark.

// Not yet testing for multi-byte characters
// Changing `r.len() == 1` to `r.chars().count() == 1` in `lint_single_char_pattern`
// should have done this but produced an ICE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a bug open for that ICE?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find one, although there may be (there are a lot of issues on rustc to filter through). It seems to happen here, on outputting the hint for x.split("💣");:

tests/compile-fail/methods.rs:377:13: 377:16 help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#single_char_pattern
tests/compile-fail/methods.rs:378:13: 378:16 error: single-character string constant used as pattern
tests/compile-fail/methods.rs:378     x.split("💣");
                                              ^~~
tests/compile-fail/methods.rs:4:9: 4:15 note: lint level defined here
tests/compile-fail/methods.rs:4 #![deny(clippy, clippy_pedantic)]
                                        ^~~~~~
tests/compile-fail/methods.rs:378:5: 378:17 help: try using a char instead:
error: internal compiler error: Error constructed but not emitted
thread panicked while panicking. aborting.

Probably related, the formatting on the hint output gets broken on multibyte chars, e.g.:

tests/compile-fail/methods.rs:377:13: 377:16 error: single-character string constant used as pattern
tests/compile-fail/methods.rs:377     x.split("ℝ");
                                              ^~~
tests/compile-fail/methods.rs:4:9: 4:15 note: lint level defined here
tests/compile-fail/methods.rs:4 #![deny(clippy, clippy_pedantic)]
                                        ^~~~~~
tests/compile-fail/methods.rs:377:5: 377:17 help: try using a char instead:
tests/compile-fail/methods.rs:        x.split('ℝ')");

llogiq added a commit that referenced this pull request Feb 16, 2016
Lint single-character strings as P: Pattern args
@llogiq llogiq merged commit 59c8f62 into rust-lang:master Feb 16, 2016
@shssoichiro shssoichiro deleted the single-char-pattern branch February 16, 2016 14:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants