Skip to content

Commit f2bef55

Browse files
committed
trait_bounds: rework two loops into one
the two loops did practically the same, only the type were different (&& vs &), so I used `copied` to convert `&&` and chained them together. Instead of parsing the trait info manually, I use the already provided method `get_trait_info_from_bound`. Also, instead of using manual string writing, I used `join` by `itertools`.
1 parent afb34eb commit f2bef55

File tree

1 file changed

+12
-24
lines changed

1 file changed

+12
-24
lines changed

clippy_lints/src/trait_bounds.rs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use clippy_utils::source::{snippet, snippet_with_applicability};
33
use clippy_utils::{SpanlessEq, SpanlessHash};
44
use core::hash::{Hash, Hasher};
55
use if_chain::if_chain;
6+
use itertools::Itertools;
67
use rustc_data_structures::fx::FxHashMap;
78
use rustc_data_structures::unhash::UnhashMap;
89
use rustc_errors::Applicability;
@@ -14,7 +15,6 @@ use rustc_hir::{
1415
use rustc_lint::{LateContext, LateLintPass};
1516
use rustc_session::{declare_tool_lint, impl_lint_pass};
1617
use rustc_span::Span;
17-
use std::fmt::Write as _;
1818

1919
declare_clippy_lint! {
2020
/// ### What it does
@@ -178,30 +178,18 @@ impl TraitBounds {
178178
);
179179

180180
then {
181-
let mut hint_string = format!(
182-
"consider combining the bounds: `{}:",
183-
snippet(cx, p.bounded_ty.span, "_")
181+
let trait_bounds = v
182+
.iter()
183+
.copied()
184+
.chain(p.bounds.iter())
185+
.filter_map(get_trait_info_from_bound)
186+
.map(|(_, _, span)| snippet_with_applicability(cx, span, "..", &mut applicability))
187+
.join(" + ");
188+
let hint_string = format!(
189+
"consider combining the bounds: `{}: {}`",
190+
snippet(cx, p.bounded_ty.span, "_"),
191+
trait_bounds,
184192
);
185-
for b in v.iter() {
186-
if let GenericBound::Trait(ref poly_trait_ref, _) = b {
187-
let path = &poly_trait_ref.trait_ref.path;
188-
let _ = write!(hint_string,
189-
" {} +",
190-
snippet_with_applicability(cx, path.span, "..", &mut applicability)
191-
);
192-
}
193-
}
194-
for b in p.bounds.iter() {
195-
if let GenericBound::Trait(ref poly_trait_ref, _) = b {
196-
let path = &poly_trait_ref.trait_ref.path;
197-
let _ = write!(hint_string,
198-
" {} +",
199-
snippet_with_applicability(cx, path.span, "..", &mut applicability)
200-
);
201-
}
202-
}
203-
hint_string.truncate(hint_string.len() - 2);
204-
hint_string.push('`');
205193
span_lint_and_help(
206194
cx,
207195
TYPE_REPETITION_IN_BOUNDS,

0 commit comments

Comments
 (0)