Clippy lints / Correctness
Clippy lint · Correctness · checked with Clippy 0.1.99
clippy::read_line_without_trim: calling `.parse()` on a string without trimming the trailing newline character
What it meansLooks for calls to [Stdin::read_line] to read a line from the standard input into a string, then later attempting to use that string for an operation that will never work for strings with a trailing newline character in it (e.g. parsing into a i32). It is in the correctness group and denied by default (the build fails).
What you see
error: calling `.parse()` on a string without trimming the trailing newline character --> main.rs:5:22 | 5 | let num: i32 = input.parse().expect("Not a number!"); | ----- ^^^^^^^ | | | 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 the checking to always fail --> main.rs:4:1 | 4 | std::io::stdin().read_line(&mut input).expect("Failed to read a line"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#read_line_without_trim = note: `#[deny(clippy::read_line_without_trim)]` on by default
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
The operation will always fail at runtime no matter what the user enters, thus making it a useless operation.
Code that is almost certainly wrong. Denied by default: the build fails until you fix it or allow it.
How to fix it
Clippy can fix this one for you: run cargo clippy --fix (add --allow-dirty if you have uncommitted changes). Review the diff before you commit.
Flagged
let mut input = String::new();
std::io::stdin().read_line(&mut input).expect("Failed to read a line");
let num: i32 = input.parse().expect("Not a number!");
assert_eq!(num, 42); // we never even get here!
Write this instead
let mut input = String::new();
std::io::stdin().read_line(&mut input).expect("Failed to read a line");
let num: i32 = input.trim_end().parse().expect("Not a number!");
// ^^^^^^^^^^^ remove the trailing newline
assert_eq!(num, 42);
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::read_line_without_trim)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
read_line_without_trim = "allow"One run
cargo clippy -- -A clippy::read-line-without-trimThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
What it does
Looks for calls to [Stdin::read_line] to read a line from the standard input
into a string, then later attempting to use that string for an operation that will never
work for strings with a trailing newline character in it (e.g. parsing into a i32).
Why is this bad?
The operation will always fail at runtime no matter what the user enters, thus making it a useless operation.
Example
let mut input = String::new();
std::io::stdin().read_line(&mut input).expect("Failed to read a line");
let num: i32 = input.parse().expect("Not a number!");
assert_eq!(num, 42); // we never even get here!
Use instead:
let mut input = String::new();
std::io::stdin().read_line(&mut input).expect("Failed to read a line");
let num: i32 = input.trim_end().parse().expect("Not a number!");
// ^^^^^^^^^^^ remove the trailing newline
assert_eq!(num, 42);
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- panicking_overflow_checksDetects C-style underflow/overflow checks
- panicking_unwrapChecks for calls of unwrap[_err]() that will always fail
- possible_missing_commapossibly missing a comma here
- recursive_format_implusing `self.to_string` in `fmt::Display` implementation will cause infinite recursion
- redundant_comparisonsChecks for ineffective double comparisons against constants
- reversed_empty_rangesthis range is empty so it will yield no values
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::panicking_overflow_checksDetects C-style underflow/overflow checksRead it next →Did this answer your question?