-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
140 additions
and
3 deletions.
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; | ||
use clippy_utils::macros::root_macro_call_first_node; | ||
use clippy_utils::ty::is_type_lang_item; | ||
use rustc_hir::Expr; | ||
use rustc_hir::ExprKind; | ||
use rustc_hir::LangItem; | ||
use rustc_lint::LateContext; | ||
use rustc_span::Span; | ||
|
||
fn tail_expr<'tcx>(expr: &'tcx Expr<'tcx>) -> Option<&'tcx Expr<'tcx>> { | ||
match expr.kind { | ||
ExprKind::Block(block, _) if !expr.span.from_expansion() => 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) = tail_expr(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,26 @@ | ||
#![allow(unused, dead_code)] | ||
#![warn(clippy::format_collect)] | ||
|
||
fn hex_encode(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,44 @@ | ||
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:19: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:20:10 | ||
| | ||
LL | .map(|s| { | ||
| ^^^ | ||
help: ... and use the `write!` macro here | ||
--> $DIR/format_collect.rs:22:13 | ||
| | ||
LL | format!("{s} {y}") | ||
| ^^^^^^^^^^^^^^^^^^ | ||
= note: this can be written more efficiently by appending to a `String` directly | ||
|
||
error: aborting due to 2 previous errors | ||
|