new lint: read_line_without_trim#10970
Merged
bors merged 2 commits intorust-lang:masterfrom Jul 5, 2023
Merged
Conversation
Collaborator
|
r? @giraffate (rustbot has picked a reviewer for you, use r? to override) |
Contributor
|
☔ The latest upstream changes (presumably #11012) made this pull request unmergeable. Please resolve the merge conflicts. |
3df67cc to
8000b44
Compare
giraffate
reviewed
Jul 3, 2023
8000b44 to
573bd75
Compare
573bd75 to
85f535b
Compare
Contributor
|
@bors r+ Thanks! |
Contributor
Contributor
Contributor
|
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
1 similar comment
Contributor
|
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
This was referenced Jul 5, 2023
bors
added a commit
that referenced
this pull request
Feb 27, 2024
[`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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds a new lint that checks for calls to
Stdin::read_linewith 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
Stdinbut 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]