-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add unused_format_specs
lint
#9637
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
+384
−28
Merged
Changes from all commits
Commits
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
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,24 @@ | ||
### What it does | ||
Detects [formatting parameters] that have no effect on the output of | ||
`format!()`, `println!()` or similar macros. | ||
|
||
### Why is this bad? | ||
Shorter format specifiers are easier to read, it may also indicate that | ||
an expected formatting operation such as adding padding isn't happening. | ||
|
||
### Example | ||
``` | ||
println!("{:.}", 1.0); | ||
|
||
println!("not padded: {:5}", format_args!("...")); | ||
``` | ||
Use instead: | ||
``` | ||
println!("{}", 1.0); | ||
|
||
println!("not padded: {}", format_args!("...")); | ||
// OR | ||
println!("padded: {:5}", format!("...")); | ||
``` | ||
|
||
[formatting parameters]: https://doc.rust-lang.org/std/fmt/index.html#formatting-parameters |
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,18 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::unused_format_specs)] | ||
#![allow(unused)] | ||
|
||
fn main() { | ||
let f = 1.0f64; | ||
println!("{}", 1.0); | ||
println!("{f} {f:?}"); | ||
|
||
println!("{}", 1); | ||
} | ||
|
||
fn should_not_lint() { | ||
let f = 1.0f64; | ||
println!("{:.1}", 1.0); | ||
println!("{f:.w$} {f:.*?}", 3, w = 2); | ||
} |
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,18 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::unused_format_specs)] | ||
#![allow(unused)] | ||
|
||
fn main() { | ||
let f = 1.0f64; | ||
println!("{:.}", 1.0); | ||
println!("{f:.} {f:.?}"); | ||
|
||
println!("{:.}", 1); | ||
} | ||
|
||
fn should_not_lint() { | ||
let f = 1.0f64; | ||
println!("{:.1}", 1.0); | ||
println!("{f:.w$} {f:.*?}", 3, w = 2); | ||
} |
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,54 @@ | ||
error: empty precision specifier has no effect | ||
--> $DIR/unused_format_specs.rs:8:17 | ||
| | ||
LL | println!("{:.}", 1.0); | ||
| ^ | ||
| | ||
= note: a precision specifier is not required to format floats | ||
= note: `-D clippy::unused-format-specs` implied by `-D warnings` | ||
help: remove the `.` | ||
| | ||
LL - println!("{:.}", 1.0); | ||
LL + println!("{}", 1.0); | ||
| | ||
|
||
error: empty precision specifier has no effect | ||
--> $DIR/unused_format_specs.rs:9:18 | ||
| | ||
LL | println!("{f:.} {f:.?}"); | ||
| ^ | ||
| | ||
= note: a precision specifier is not required to format floats | ||
help: remove the `.` | ||
| | ||
LL - println!("{f:.} {f:.?}"); | ||
LL + println!("{f} {f:.?}"); | ||
| | ||
|
||
error: empty precision specifier has no effect | ||
--> $DIR/unused_format_specs.rs:9:24 | ||
| | ||
LL | println!("{f:.} {f:.?}"); | ||
| ^ | ||
| | ||
= note: a precision specifier is not required to format floats | ||
help: remove the `.` | ||
| | ||
LL - println!("{f:.} {f:.?}"); | ||
LL + println!("{f:.} {f:?}"); | ||
| | ||
|
||
error: empty precision specifier has no effect | ||
--> $DIR/unused_format_specs.rs:11:17 | ||
| | ||
LL | println!("{:.}", 1); | ||
| ^ | ||
| | ||
help: remove the `.` | ||
| | ||
LL - println!("{:.}", 1); | ||
LL + println!("{}", 1); | ||
| | ||
|
||
error: aborting due to 4 previous errors | ||
|
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,30 @@ | ||
#![warn(clippy::unused_format_specs)] | ||
#![allow(unused)] | ||
|
||
macro_rules! format_args_from_macro { | ||
() => { | ||
format_args!("from macro") | ||
}; | ||
} | ||
|
||
fn main() { | ||
// prints `.`, not ` .` | ||
println!("{:5}.", format_args!("")); | ||
//prints `abcde`, not `abc` | ||
println!("{:.3}", format_args!("abcde")); | ||
|
||
println!("{:5}.", format_args_from_macro!()); | ||
|
||
let args = format_args!(""); | ||
println!("{args:5}"); | ||
} | ||
|
||
fn should_not_lint() { | ||
println!("{}", format_args!("")); | ||
// Technically the same as `{}`, but the `format_args` docs specifically mention that you can use | ||
// debug formatting so allow it | ||
println!("{:?}", format_args!("")); | ||
|
||
let args = format_args!(""); | ||
println!("{args}"); | ||
} |
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,69 @@ | ||
error: format specifiers have no effect on `format_args!()` | ||
--> $DIR/unused_format_specs_unfixable.rs:12:15 | ||
| | ||
LL | println!("{:5}.", format_args!("")); | ||
| ^^^^ | ||
| | ||
= note: `-D clippy::unused-format-specs` implied by `-D warnings` | ||
help: for the width to apply consider using `format!()` | ||
| | ||
LL | println!("{:5}.", format!("")); | ||
| ~~~~~~ | ||
help: if the current behavior is intentional, remove the format specifiers | ||
| | ||
LL - println!("{:5}.", format_args!("")); | ||
LL + println!("{}.", format_args!("")); | ||
| | ||
|
||
error: format specifiers have no effect on `format_args!()` | ||
--> $DIR/unused_format_specs_unfixable.rs:14:15 | ||
| | ||
LL | println!("{:.3}", format_args!("abcde")); | ||
| ^^^^^ | ||
| | ||
help: for the precision to apply consider using `format!()` | ||
| | ||
LL | println!("{:.3}", format!("abcde")); | ||
| ~~~~~~ | ||
help: if the current behavior is intentional, remove the format specifiers | ||
| | ||
LL - println!("{:.3}", format_args!("abcde")); | ||
LL + println!("{}", format_args!("abcde")); | ||
| | ||
|
||
error: format specifiers have no effect on `format_args!()` | ||
--> $DIR/unused_format_specs_unfixable.rs:16:15 | ||
| | ||
LL | println!("{:5}.", format_args_from_macro!()); | ||
| ^^^^ | ||
| | ||
help: for the width to apply consider using `format!()` | ||
--> $DIR/unused_format_specs_unfixable.rs:16:17 | ||
| | ||
LL | println!("{:5}.", format_args_from_macro!()); | ||
| ^ | ||
help: if the current behavior is intentional, remove the format specifiers | ||
| | ||
LL - println!("{:5}.", format_args_from_macro!()); | ||
LL + println!("{}.", format_args_from_macro!()); | ||
| | ||
|
||
error: format specifiers have no effect on `format_args!()` | ||
--> $DIR/unused_format_specs_unfixable.rs:19:15 | ||
| | ||
LL | println!("{args:5}"); | ||
| ^^^^^^^^ | ||
| | ||
help: for the width to apply consider using `format!()` | ||
--> $DIR/unused_format_specs_unfixable.rs:19:21 | ||
| | ||
LL | println!("{args:5}"); | ||
| ^ | ||
help: if the current behavior is intentional, remove the format specifiers | ||
| | ||
LL - println!("{args:5}"); | ||
LL + println!("{args}"); | ||
| | ||
|
||
error: aborting due to 4 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.