-
Notifications
You must be signed in to change notification settings - Fork 1.7k
unnecessary_join
lint
#8573
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
Closed
Closed
unnecessary_join
lint
#8573
Changes from all commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
5210a05
add unnecessary join lint
yoav-lavi 0cc0d3d
Merge branch 'rust-lang:master' into master
yoav-lavi 4546e1c
update lints
yoav-lavi 3c8a92d
Merge branch 'master' of https://github.com/yoav-lavi/rust-clippy
yoav-lavi b47aefd
fix description
yoav-lavi 40f2e0e
fix description
yoav-lavi 12a4632
cleanup
yoav-lavi a03ee32
fixes
yoav-lavi d801dd4
fix desc
yoav-lavi d34f5db
move comment
yoav-lavi 907e913
change explanation
yoav-lavi 9ff0ddd
Update tests/ui/unnecessary_join.rs
yoav-lavi 3fd4266
fix redundant message
yoav-lavi 345cde6
Merge branch 'master' of https://github.com/yoav-lavi/rust-clippy
yoav-lavi 6c02771
add version
yoav-lavi ac1fab4
move up check
yoav-lavi 98c8c26
pass arguments, fix span
yoav-lavi ee7791b
cleanup
yoav-lavi 3322ba9
fix spans
yoav-lavi 52f47ef
cleanup
yoav-lavi 7b40cfa
update test
yoav-lavi 14405eb
move lets out of if chain
yoav-lavi 8d41a8f
fix span
yoav-lavi bcdc558
remove intellij settings
yoav-lavi b4fa64d
fix message
yoav-lavi 78c107b
cleanup
yoav-lavi caddb9f
cleanup
yoav-lavi 86b6523
update lints
yoav-lavi dccddd6
temporarily remove unknown lint
yoav-lavi c4de8cf
update tests
yoav-lavi fbe086f
cleanup
yoav-lavi 95a7aa2
remove unneeded ref
yoav-lavi 88be265
order arguments
yoav-lavi 509031f
normalize argument names
yoav-lavi ecfffb1
cleanup
yoav-lavi 53dfc75
fix version
yoav-lavi 61bc156
switch to pedantic, add warning
yoav-lavi 8f706ef
fix tests and update lints
yoav-lavi 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use clippy_utils::{diagnostics::span_lint_and_sugg, ty::is_type_diagnostic_item}; | ||
use rustc_ast::ast::LitKind; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_middle::ty::{Ref, Slice}; | ||
use rustc_span::{sym, Span}; | ||
|
||
use super::UNNECESSARY_JOIN; | ||
|
||
pub(super) fn check<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx Expr<'tcx>, | ||
join_self_arg: &'tcx Expr<'tcx>, | ||
join_arg: &'tcx Expr<'tcx>, | ||
span: Span, | ||
) { | ||
let applicability = Applicability::MachineApplicable; | ||
let collect_output_adjusted_type = cx.typeck_results().expr_ty_adjusted(join_self_arg); | ||
if_chain! { | ||
// the turbofish for collect is ::<Vec<String>> | ||
if let Ref(_, ref_type, _) = collect_output_adjusted_type.kind(); | ||
if let Slice(slice) = ref_type.kind(); | ||
if is_type_diagnostic_item(cx, *slice, sym::String); | ||
// the argument for join is "" | ||
if let ExprKind::Lit(spanned) = &join_arg.kind; | ||
if let LitKind::Str(symbol, _) = spanned.node; | ||
if symbol.is_empty(); | ||
then { | ||
span_lint_and_sugg( | ||
cx, | ||
UNNECESSARY_JOIN, | ||
span.with_hi(expr.span.hi()), | ||
r#"called `.collect<Vec<String>>().join("")` on an iterator"#, | ||
"try using", | ||
"collect::<String>()".to_owned(), | ||
applicability, | ||
); | ||
} | ||
} | ||
} |
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,35 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::unnecessary_join)] | ||
|
||
fn main() { | ||
// should be linted | ||
let vector = vec!["hello", "world"]; | ||
let output = vector | ||
.iter() | ||
.map(|item| item.to_uppercase()) | ||
.collect::<String>(); | ||
println!("{}", output); | ||
|
||
// should be linted | ||
let vector = vec!["hello", "world"]; | ||
let output = vector | ||
.iter() | ||
.map(|item| item.to_uppercase()) | ||
.collect::<String>(); | ||
println!("{}", output); | ||
|
||
// should not be linted | ||
let vector = vec!["hello", "world"]; | ||
let output = vector | ||
.iter() | ||
.map(|item| item.to_uppercase()) | ||
.collect::<Vec<String>>() | ||
.join("\n"); | ||
println!("{}", output); | ||
|
||
// should not be linted | ||
let vector = vec!["hello", "world"]; | ||
let output = vector.iter().map(|item| item.to_uppercase()).collect::<String>(); | ||
println!("{}", output); | ||
} |
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,37 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::unnecessary_join)] | ||
|
||
fn main() { | ||
// should be linted | ||
let vector = vec!["hello", "world"]; | ||
let output = vector | ||
.iter() | ||
.map(|item| item.to_uppercase()) | ||
.collect::<Vec<String>>() | ||
.join(""); | ||
println!("{}", output); | ||
|
||
// should be linted | ||
let vector = vec!["hello", "world"]; | ||
let output = vector | ||
.iter() | ||
.map(|item| item.to_uppercase()) | ||
.collect::<Vec<_>>() | ||
.join(""); | ||
println!("{}", output); | ||
|
||
// should not be linted | ||
let vector = vec!["hello", "world"]; | ||
let output = vector | ||
.iter() | ||
.map(|item| item.to_uppercase()) | ||
.collect::<Vec<String>>() | ||
.join("\n"); | ||
println!("{}", output); | ||
|
||
// should not be linted | ||
let vector = vec!["hello", "world"]; | ||
let output = vector.iter().map(|item| item.to_uppercase()).collect::<String>(); | ||
println!("{}", output); | ||
} |
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,20 @@ | ||
error: called `.collect<Vec<String>>().join("")` on an iterator | ||
--> $DIR/unnecessary_join.rs:11:10 | ||
| | ||
LL | .collect::<Vec<String>>() | ||
| __________^ | ||
LL | | .join(""); | ||
| |_________________^ help: try using: `collect::<String>()` | ||
| | ||
= note: `-D clippy::unnecessary-join` implied by `-D warnings` | ||
|
||
error: called `.collect<Vec<String>>().join("")` on an iterator | ||
--> $DIR/unnecessary_join.rs:20:10 | ||
| | ||
LL | .collect::<Vec<_>>() | ||
| __________^ | ||
LL | | .join(""); | ||
| |_________________^ help: try using: `collect::<String>()` | ||
|
||
error: aborting due to 2 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.