-
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: format_collect
#11116
Merged
Merged
new lint: format_collect
#11116
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 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 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 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,33 @@ | ||
use super::FORMAT_COLLECT; | ||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use clippy_utils::macros::{is_format_macro, root_macro_call_first_node}; | ||
use clippy_utils::ty::is_type_lang_item; | ||
use rustc_hir::{Expr, ExprKind, LangItem}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::Span; | ||
|
||
/// Same as `peel_blocks` but only actually considers blocks that are not from an expansion. | ||
/// This is needed because always calling `peel_blocks` would otherwise remove parts of the | ||
/// `format!` macro, which would cause `root_macro_call_first_node` to return `None`. | ||
fn peel_non_expn_blocks<'tcx>(expr: &'tcx Expr<'tcx>) -> Option<&'tcx Expr<'tcx>> { | ||
match expr.kind { | ||
ExprKind::Block(block, _) if !expr.span.from_expansion() => peel_non_expn_blocks(block.expr?), | ||
_ => Some(expr), | ||
} | ||
} | ||
|
||
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, map_arg: &Expr<'_>, map_span: Span) { | ||
if is_type_lang_item(cx, cx.typeck_results().expr_ty(expr), LangItem::String) | ||
&& let ExprKind::Closure(closure) = map_arg.kind | ||
&& let body = cx.tcx.hir().body(closure.body) | ||
&& let Some(value) = peel_non_expn_blocks(body.value) | ||
&& let Some(mac) = root_macro_call_first_node(cx, value) | ||
&& is_format_macro(cx, mac.def_id) | ||
{ | ||
span_lint_and_then(cx, FORMAT_COLLECT, expr.span, "use of `format!` to build up a string from an iterator", |diag| { | ||
diag.span_help(map_span, "call `fold` instead") | ||
.span_help(value.span.source_callsite(), "... and use the `write!` macro here") | ||
.note("this can be written more efficiently by appending to a `String` directly"); | ||
}); | ||
} | ||
} |
This file contains 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 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 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,31 @@ | ||
#![allow(unused, dead_code)] | ||
#![warn(clippy::format_collect)] | ||
|
||
fn hex_encode(bytes: &[u8]) -> String { | ||
bytes.iter().map(|b| format!("{b:02X}")).collect() | ||
} | ||
|
||
#[rustfmt::skip] | ||
fn hex_encode_deep(bytes: &[u8]) -> String { | ||
bytes.iter().map(|b| {{{{{ format!("{b:02X}") }}}}}).collect() | ||
} | ||
|
||
macro_rules! fmt { | ||
($x:ident) => { | ||
format!("{x:02X}", x = $x) | ||
}; | ||
} | ||
|
||
fn from_macro(bytes: &[u8]) -> String { | ||
bytes.iter().map(|x| fmt!(x)).collect() | ||
} | ||
|
||
fn with_block() -> String { | ||
(1..10) | ||
.map(|s| { | ||
let y = 1; | ||
format!("{s} {y}") | ||
}) | ||
.collect() | ||
} | ||
fn main() {} |
This file contains 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,62 @@ | ||
error: use of `format!` to build up a string from an iterator | ||
--> $DIR/format_collect.rs:5:5 | ||
| | ||
LL | bytes.iter().map(|b| format!("{b:02X}")).collect() | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
help: call `fold` instead | ||
--> $DIR/format_collect.rs:5:18 | ||
| | ||
LL | bytes.iter().map(|b| format!("{b:02X}")).collect() | ||
| ^^^ | ||
help: ... and use the `write!` macro here | ||
--> $DIR/format_collect.rs:5:26 | ||
| | ||
LL | bytes.iter().map(|b| format!("{b:02X}")).collect() | ||
| ^^^^^^^^^^^^^^^^^^ | ||
= note: this can be written more efficiently by appending to a `String` directly | ||
= note: `-D clippy::format-collect` implied by `-D warnings` | ||
|
||
error: use of `format!` to build up a string from an iterator | ||
--> $DIR/format_collect.rs:10:5 | ||
| | ||
LL | bytes.iter().map(|b| {{{{{ format!("{b:02X}") }}}}}).collect() | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
help: call `fold` instead | ||
--> $DIR/format_collect.rs:10:18 | ||
| | ||
LL | bytes.iter().map(|b| {{{{{ format!("{b:02X}") }}}}}).collect() | ||
| ^^^ | ||
help: ... and use the `write!` macro here | ||
--> $DIR/format_collect.rs:10:32 | ||
| | ||
LL | bytes.iter().map(|b| {{{{{ format!("{b:02X}") }}}}}).collect() | ||
| ^^^^^^^^^^^^^^^^^^ | ||
= note: this can be written more efficiently by appending to a `String` directly | ||
|
||
error: use of `format!` to build up a string from an iterator | ||
--> $DIR/format_collect.rs:24:5 | ||
| | ||
LL | / (1..10) | ||
LL | | .map(|s| { | ||
LL | | let y = 1; | ||
LL | | format!("{s} {y}") | ||
LL | | }) | ||
LL | | .collect() | ||
| |__________________^ | ||
| | ||
help: call `fold` instead | ||
--> $DIR/format_collect.rs:25:10 | ||
| | ||
LL | .map(|s| { | ||
| ^^^ | ||
help: ... and use the `write!` macro here | ||
--> $DIR/format_collect.rs:27:13 | ||
| | ||
LL | format!("{s} {y}") | ||
| ^^^^^^^^^^^^^^^^^^ | ||
= note: this can be written more efficiently by appending to a `String` directly | ||
|
||
error: aborting due to 3 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure whether this should be
perf
. It's faster, but the example given doesn't seem to be worth the code smell (it should also probably usetry_fold
so errors fromwrite!
are propagated)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
write!
never errors onString
s, so I'm not too sure on that. But yes, I agree that this might be too annoying forperf
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting, that should probably be documented in the lint description with a comment then as that's rather unexpected behavior (at least from the reader's perspective)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it's only really a useful suggestion if the iterator is gonna yield lots of small items, like bytes or chars (which seems hard to put into a lint, other than maybe restricting it to small types), otherwise it feels pedantic, like here: https://github.com/rust-lang/rust-clippy/pull/11116/files#diff-42cb6807ad74b3e201c5a7ca98b911c5fa08380e942be6e4ac5807f8377f87fcL135
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Display
impls can return errors even ifString
does not (which is not advisable though)