-
Notifications
You must be signed in to change notification settings - Fork 1.7k
new lint: read_line_without_trim
#10970
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use std::ops::ControlFlow; | ||
|
||
use clippy_utils::{ | ||
diagnostics::span_lint_and_then, get_parent_expr, match_def_path, source::snippet, ty::is_type_diagnostic_item, | ||
visitors::for_each_local_use_after_expr, | ||
}; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::Expr; | ||
use rustc_hir::QPath; | ||
use rustc_hir::{def::Res, ExprKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_middle::ty::{self, Ty}; | ||
use rustc_span::sym; | ||
|
||
use super::READ_LINE_WITHOUT_TRIM; | ||
|
||
/// Will a `.parse::<ty>()` call fail if the input has a trailing newline? | ||
fn parse_fails_on_trailing_newline(ty: Ty<'_>) -> bool { | ||
// only allow a very limited set of types for now, for which we 100% know parsing will fail | ||
matches!(ty.kind(), ty::Float(_) | ty::Bool | ty::Int(_) | ty::Uint(_)) | ||
} | ||
|
||
pub fn check(cx: &LateContext<'_>, call: &Expr<'_>, recv: &Expr<'_>, arg: &Expr<'_>) { | ||
if let Some(recv_adt) = cx.typeck_results().expr_ty(recv).ty_adt_def() | ||
&& match_def_path(cx, recv_adt.did(), &["std", "io", "stdio", "Stdin"]) | ||
&& let ExprKind::Path(QPath::Resolved(_, path)) = arg.peel_borrows().kind | ||
&& let Res::Local(local_id) = path.res | ||
{ | ||
// We've checked that `call` is a call to `Stdin::read_line()` with the right receiver, | ||
// now let's check if the first use of the string passed to `::read_line()` is | ||
// parsed into a type that will always fail if it has a trailing newline. | ||
for_each_local_use_after_expr(cx, local_id, call.hir_id, |expr| { | ||
if let Some(parent) = get_parent_expr(cx, expr) | ||
&& let ExprKind::MethodCall(segment, .., span) = parent.kind | ||
&& segment.ident.name == sym!(parse) | ||
&& let parse_result_ty = cx.typeck_results().expr_ty(parent) | ||
&& is_type_diagnostic_item(cx, parse_result_ty, sym::Result) | ||
&& let ty::Adt(_, substs) = parse_result_ty.kind() | ||
&& let Some(ok_ty) = substs[0].as_type() | ||
&& parse_fails_on_trailing_newline(ok_ty) | ||
{ | ||
let local_snippet = snippet(cx, expr.span, "<expr>"); | ||
span_lint_and_then( | ||
cx, | ||
READ_LINE_WITHOUT_TRIM, | ||
span, | ||
"calling `.parse()` without trimming the trailing newline character", | ||
|diag| { | ||
diag.span_note(call.span, "call to `.read_line()` here, \ | ||
which leaves a trailing newline character in the buffer, \ | ||
which in turn will cause `.parse()` to fail"); | ||
|
||
diag.span_suggestion( | ||
expr.span, | ||
"try", | ||
format!("{local_snippet}.trim_end()"), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
); | ||
} | ||
|
||
// only consider the first use to prevent this scenario: | ||
// ``` | ||
// let mut s = String::new(); | ||
// std::io::stdin().read_line(&mut s); | ||
// s.pop(); | ||
// let _x: i32 = s.parse().unwrap(); | ||
// ``` | ||
// this is actually fine, because the pop call removes the trailing newline. | ||
ControlFlow::<(), ()>::Break(()) | ||
}); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//@run-rustfix | ||
|
||
#![allow(unused)] | ||
#![warn(clippy::read_line_without_trim)] | ||
|
||
fn main() { | ||
let mut input = String::new(); | ||
std::io::stdin().read_line(&mut input).unwrap(); | ||
input.pop(); | ||
let _x: i32 = input.parse().unwrap(); // don't trigger here, newline character is popped | ||
|
||
let mut input = String::new(); | ||
std::io::stdin().read_line(&mut input).unwrap(); | ||
let _x: i32 = input.trim_end().parse().unwrap(); | ||
|
||
let mut input = String::new(); | ||
std::io::stdin().read_line(&mut input).unwrap(); | ||
let _x = input.trim_end().parse::<i32>().unwrap(); | ||
|
||
let mut input = String::new(); | ||
std::io::stdin().read_line(&mut input).unwrap(); | ||
let _x = input.trim_end().parse::<u32>().unwrap(); | ||
|
||
let mut input = String::new(); | ||
std::io::stdin().read_line(&mut input).unwrap(); | ||
let _x = input.trim_end().parse::<f32>().unwrap(); | ||
|
||
let mut input = String::new(); | ||
std::io::stdin().read_line(&mut input).unwrap(); | ||
let _x = input.trim_end().parse::<bool>().unwrap(); | ||
|
||
let mut input = String::new(); | ||
std::io::stdin().read_line(&mut input).unwrap(); | ||
// this is actually ok, so don't lint here | ||
let _x = input.parse::<String>().unwrap(); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//@run-rustfix | ||
|
||
#![allow(unused)] | ||
#![warn(clippy::read_line_without_trim)] | ||
|
||
fn main() { | ||
let mut input = String::new(); | ||
std::io::stdin().read_line(&mut input).unwrap(); | ||
input.pop(); | ||
let _x: i32 = input.parse().unwrap(); // don't trigger here, newline character is popped | ||
|
||
let mut input = String::new(); | ||
std::io::stdin().read_line(&mut input).unwrap(); | ||
let _x: i32 = input.parse().unwrap(); | ||
|
||
let mut input = String::new(); | ||
std::io::stdin().read_line(&mut input).unwrap(); | ||
let _x = input.parse::<i32>().unwrap(); | ||
|
||
let mut input = String::new(); | ||
std::io::stdin().read_line(&mut input).unwrap(); | ||
let _x = input.parse::<u32>().unwrap(); | ||
|
||
let mut input = String::new(); | ||
std::io::stdin().read_line(&mut input).unwrap(); | ||
let _x = input.parse::<f32>().unwrap(); | ||
|
||
let mut input = String::new(); | ||
std::io::stdin().read_line(&mut input).unwrap(); | ||
let _x = input.parse::<bool>().unwrap(); | ||
|
||
let mut input = String::new(); | ||
std::io::stdin().read_line(&mut input).unwrap(); | ||
// this is actually ok, so don't lint here | ||
let _x = input.parse::<String>().unwrap(); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
error: calling `.parse()` without trimming the trailing newline character | ||
--> $DIR/read_line_without_trim.rs:14:25 | ||
| | ||
LL | let _x: i32 = input.parse().unwrap(); | ||
| ----- ^^^^^^^ | ||
| | | ||
| help: try: `input.trim_end()` | ||
| | ||
note: call to `.read_line()` here, which leaves a trailing newline character in the buffer, which in turn will cause `.parse()` to fail | ||
--> $DIR/read_line_without_trim.rs:13:5 | ||
| | ||
LL | std::io::stdin().read_line(&mut input).unwrap(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
= note: `-D clippy::read-line-without-trim` implied by `-D warnings` | ||
|
||
error: calling `.parse()` without trimming the trailing newline character | ||
--> $DIR/read_line_without_trim.rs:18:20 | ||
| | ||
LL | let _x = input.parse::<i32>().unwrap(); | ||
| ----- ^^^^^^^^^^^^^^ | ||
| | | ||
| help: try: `input.trim_end()` | ||
| | ||
note: call to `.read_line()` here, which leaves a trailing newline character in the buffer, which in turn will cause `.parse()` to fail | ||
--> $DIR/read_line_without_trim.rs:17:5 | ||
| | ||
LL | std::io::stdin().read_line(&mut input).unwrap(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: calling `.parse()` without trimming the trailing newline character | ||
--> $DIR/read_line_without_trim.rs:22:20 | ||
| | ||
LL | let _x = input.parse::<u32>().unwrap(); | ||
| ----- ^^^^^^^^^^^^^^ | ||
| | | ||
| help: try: `input.trim_end()` | ||
| | ||
note: call to `.read_line()` here, which leaves a trailing newline character in the buffer, which in turn will cause `.parse()` to fail | ||
--> $DIR/read_line_without_trim.rs:21:5 | ||
| | ||
LL | std::io::stdin().read_line(&mut input).unwrap(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: calling `.parse()` without trimming the trailing newline character | ||
--> $DIR/read_line_without_trim.rs:26:20 | ||
| | ||
LL | let _x = input.parse::<f32>().unwrap(); | ||
| ----- ^^^^^^^^^^^^^^ | ||
| | | ||
| help: try: `input.trim_end()` | ||
| | ||
note: call to `.read_line()` here, which leaves a trailing newline character in the buffer, which in turn will cause `.parse()` to fail | ||
--> $DIR/read_line_without_trim.rs:25:5 | ||
| | ||
LL | std::io::stdin().read_line(&mut input).unwrap(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: calling `.parse()` without trimming the trailing newline character | ||
--> $DIR/read_line_without_trim.rs:30:20 | ||
| | ||
LL | let _x = input.parse::<bool>().unwrap(); | ||
| ----- ^^^^^^^^^^^^^^^ | ||
| | | ||
| help: try: `input.trim_end()` | ||
| | ||
note: call to `.read_line()` here, which leaves a trailing newline character in the buffer, which in turn will cause `.parse()` to fail | ||
--> $DIR/read_line_without_trim.rs:29:5 | ||
| | ||
LL | std::io::stdin().read_line(&mut input).unwrap(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 5 previous errors | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.