-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
new lint: read_line_without_trim
#10970
Conversation
r? @giraffate (rustbot has picked a reviewer for you, use r? to override) |
☔ The latest upstream changes (presumably #11012) made this pull request unmergeable. Please resolve the merge conflicts. |
3df67cc
to
8000b44
Compare
8000b44
to
573bd75
Compare
573bd75
to
85f535b
Compare
@bors r+ Thanks! |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
1 similar comment
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
[`read_line_without_trim`]: detect string literal comparison and `.ends_with()` calls This lint now also realizes that a comparison like `s == "foo"` and calls such as `s.ends_with("foo")` will fail if `s` was initialized by a call to `Stdin::read_line` (because of the trailing newline). changelog: [`read_line_without_trim`]: detect string literal comparison and `.ends_with()` calls r? `@giraffate` assigning you because you reviewed #10970 that added this lint, so this is kinda a followup PR ^^
This adds a new lint that checks for calls to
Stdin::read_line
with a reference to a string that is then attempted to parse into an integer type without first trimming it, which is always going to fail at runtime.This is something that I've seen happen a lot to beginners, because it's easy to run into when following the example of chapter 2 in the book where it shows how to program a guessing game.
It would be nice if we could point beginners to clippy and tell them "let's see what clippy has to say" and have clippy explain to them why it fails 👀
I think this lint can later be "generalized" to work not just for
Stdin
but also anyBufRead
(which seems to be where the guarantee about the trailing newline comes from) and also, matching/comparing it to a string slice that doesn't end in a newline character (e.g.input == "foo"
is always going to fail)changelog: new lint: [
read_line_without_trim
]