2021-03-16 00:55:45 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_help;
|
2021-03-14 23:17:44 +00:00
|
|
|
use clippy_utils::source::{snippet, snippet_with_applicability};
|
2022-01-04 19:20:27 +00:00
|
|
|
use clippy_utils::{SpanlessEq, SpanlessHash};
|
|
|
|
use core::hash::{Hash, Hasher};
|
2020-07-03 09:48:28 +00:00
|
|
|
use if_chain::if_chain;
|
2019-02-15 20:21:13 +00:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2021-05-12 15:50:19 +00:00
|
|
|
use rustc_data_structures::unhash::UnhashMap;
|
2020-03-27 14:34:29 +00:00
|
|
|
use rustc_errors::Applicability;
|
2022-01-04 19:20:27 +00:00
|
|
|
use rustc_hir::{def::Res, GenericBound, Generics, ParamName, Path, QPath, Ty, TyKind, WherePredicate};
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2020-07-28 14:23:47 +00:00
|
|
|
use rustc_span::Span;
|
2019-02-15 20:21:13 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-02 18:37:11 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// This lint warns about unnecessary type repetitions in trait bounds
|
2019-07-27 11:06:25 +00:00
|
|
|
///
|
2021-07-02 18:37:11 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Repeating the type for every bound makes the code
|
2019-07-27 20:58:29 +00:00
|
|
|
/// less readable than combining the bounds
|
2019-07-27 11:06:25 +00:00
|
|
|
///
|
2021-07-02 18:37:11 +00:00
|
|
|
/// ### Example
|
2019-07-27 11:06:25 +00:00
|
|
|
/// ```rust
|
2019-08-02 06:13:54 +00:00
|
|
|
/// pub fn foo<T>(t: T) where T: Copy, T: Clone {}
|
2019-07-27 11:06:25 +00:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Could be written as:
|
|
|
|
///
|
|
|
|
/// ```rust
|
2019-08-02 06:13:54 +00:00
|
|
|
/// pub fn foo<T>(t: T) where T: Copy + Clone {}
|
2019-07-27 11:06:25 +00:00
|
|
|
/// ```
|
Added `clippy::version` attribute to all normal lints
So, some context for this, well, more a story. I'm not used to scripting, I've never really scripted anything, even if it's a valuable skill. I just never really needed it. Now, `@flip1995` correctly suggested using a script for this in `rust-clippy#7813`...
And I decided to write a script using nushell because why not? This was a mistake... I spend way more time on this than I would like to admit. It has definitely been more than 4 hours. It shouldn't take that long, but me being new to scripting and nushell just wasn't a good mixture... Anyway, here is the script that creates another script which adds the versions. Fun...
Just execute this on the `gh-pages` branch and the resulting `replacer.sh` in `clippy_lints` and it should all work.
```nu
mv v0.0.212 rust-1.00.0;
mv beta rust-1.57.0;
mv master rust-1.58.0;
let paths = (open ./rust-1.58.0/lints.json | select id id_span | flatten | select id path);
let versions = (
ls | where name =~ "rust-" | select name | format {name}/lints.json |
each { open $it | select id | insert version $it | str substring "5,11" version} |
group-by id | rotate counter-clockwise id version |
update version {get version | first 1} | flatten | select id version);
$paths | each { |row|
let version = ($versions | where id == ($row.id) | format {version})
let idu = ($row.id | str upcase)
$"sed -i '0,/($idu),/{s/pub ($idu),/#[clippy::version = "($version)"]\n pub ($idu),/}' ($row.path)"
} | str collect ";" | str find-replace --all '1.00.0' 'pre 1.29.0' | save "replacer.sh";
```
And this still has some problems, but at this point I just want to be done -.-
2021-10-21 19:06:26 +00:00
|
|
|
#[clippy::version = "1.38.0"]
|
2019-02-15 20:21:13 +00:00
|
|
|
pub TYPE_REPETITION_IN_BOUNDS,
|
2019-08-17 10:45:05 +00:00
|
|
|
pedantic,
|
2019-02-15 20:21:13 +00:00
|
|
|
"Types are repeated unnecessary in trait bounds use `+` instead of using `T: _, T: _`"
|
|
|
|
}
|
|
|
|
|
2020-07-28 14:23:47 +00:00
|
|
|
declare_clippy_lint! {
|
2021-07-02 18:37:11 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for cases where generics are being used and multiple
|
2020-07-28 14:23:47 +00:00
|
|
|
/// syntax specifications for trait bounds are used simultaneously.
|
|
|
|
///
|
2021-07-02 18:37:11 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Duplicate bounds makes the code
|
2020-07-28 14:23:47 +00:00
|
|
|
/// less readable than specifing them only once.
|
|
|
|
///
|
2021-07-02 18:37:11 +00:00
|
|
|
/// ### Example
|
2020-07-28 14:23:47 +00:00
|
|
|
/// ```rust
|
|
|
|
/// fn func<T: Clone + Default>(arg: T) where T: Clone + Default {}
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Could be written as:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// fn func<T: Clone + Default>(arg: T) {}
|
|
|
|
/// ```
|
|
|
|
/// or
|
2020-08-04 18:23:14 +00:00
|
|
|
///
|
2020-07-28 14:23:47 +00:00
|
|
|
/// ```rust
|
|
|
|
/// fn func<T>(arg: T) where T: Clone + Default {}
|
|
|
|
/// ```
|
Added `clippy::version` attribute to all normal lints
So, some context for this, well, more a story. I'm not used to scripting, I've never really scripted anything, even if it's a valuable skill. I just never really needed it. Now, `@flip1995` correctly suggested using a script for this in `rust-clippy#7813`...
And I decided to write a script using nushell because why not? This was a mistake... I spend way more time on this than I would like to admit. It has definitely been more than 4 hours. It shouldn't take that long, but me being new to scripting and nushell just wasn't a good mixture... Anyway, here is the script that creates another script which adds the versions. Fun...
Just execute this on the `gh-pages` branch and the resulting `replacer.sh` in `clippy_lints` and it should all work.
```nu
mv v0.0.212 rust-1.00.0;
mv beta rust-1.57.0;
mv master rust-1.58.0;
let paths = (open ./rust-1.58.0/lints.json | select id id_span | flatten | select id path);
let versions = (
ls | where name =~ "rust-" | select name | format {name}/lints.json |
each { open $it | select id | insert version $it | str substring "5,11" version} |
group-by id | rotate counter-clockwise id version |
update version {get version | first 1} | flatten | select id version);
$paths | each { |row|
let version = ($versions | where id == ($row.id) | format {version})
let idu = ($row.id | str upcase)
$"sed -i '0,/($idu),/{s/pub ($idu),/#[clippy::version = "($version)"]\n pub ($idu),/}' ($row.path)"
} | str collect ";" | str find-replace --all '1.00.0' 'pre 1.29.0' | save "replacer.sh";
```
And this still has some problems, but at this point I just want to be done -.-
2021-10-21 19:06:26 +00:00
|
|
|
#[clippy::version = "1.47.0"]
|
2020-07-28 14:23:47 +00:00
|
|
|
pub TRAIT_DUPLICATION_IN_BOUNDS,
|
|
|
|
pedantic,
|
2020-07-29 14:10:15 +00:00
|
|
|
"Check if the same trait bounds are specified twice during a function declaration"
|
2020-07-28 14:23:47 +00:00
|
|
|
}
|
|
|
|
|
2020-06-28 10:14:04 +00:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct TraitBounds {
|
|
|
|
max_trait_bounds: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TraitBounds {
|
|
|
|
#[must_use]
|
|
|
|
pub fn new(max_trait_bounds: u64) -> Self {
|
|
|
|
Self { max_trait_bounds }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-28 14:23:47 +00:00
|
|
|
impl_lint_pass!(TraitBounds => [TYPE_REPETITION_IN_BOUNDS, TRAIT_DUPLICATION_IN_BOUNDS]);
|
2019-02-15 20:21:13 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for TraitBounds {
|
|
|
|
fn check_generics(&mut self, cx: &LateContext<'tcx>, gen: &'tcx Generics<'_>) {
|
2020-07-28 14:23:47 +00:00
|
|
|
self.check_type_repetition(cx, gen);
|
|
|
|
check_trait_bound_duplication(cx, gen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_trait_res_span_from_bound(bound: &GenericBound<'_>) -> Option<(Res, Span)> {
|
|
|
|
if let GenericBound::Trait(t, _) = bound {
|
|
|
|
Some((t.trait_ref.path.res, t.span))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TraitBounds {
|
2022-01-04 19:20:27 +00:00
|
|
|
fn check_type_repetition<'tcx>(self, cx: &LateContext<'tcx>, gen: &'tcx Generics<'_>) {
|
|
|
|
struct SpanlessTy<'cx, 'tcx> {
|
|
|
|
ty: &'tcx Ty<'tcx>,
|
|
|
|
cx: &'cx LateContext<'tcx>,
|
|
|
|
}
|
|
|
|
impl PartialEq for SpanlessTy<'_, '_> {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
let mut eq = SpanlessEq::new(self.cx);
|
|
|
|
eq.inter_expr().eq_ty(self.ty, other.ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Hash for SpanlessTy<'_, '_> {
|
|
|
|
fn hash<H: Hasher>(&self, h: &mut H) {
|
|
|
|
let mut t = SpanlessHash::new(self.cx);
|
|
|
|
t.hash_ty(self.ty);
|
|
|
|
h.write_u64(t.finish());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Eq for SpanlessTy<'_, '_> {}
|
|
|
|
|
2021-10-29 16:14:22 +00:00
|
|
|
if gen.span.from_expansion() {
|
2019-02-15 20:21:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-01-04 19:20:27 +00:00
|
|
|
let mut map: UnhashMap<SpanlessTy<'_, '_>, Vec<&GenericBound<'_>>> = UnhashMap::default();
|
2020-03-27 14:34:29 +00:00
|
|
|
let mut applicability = Applicability::MaybeIncorrect;
|
2019-12-30 04:02:10 +00:00
|
|
|
for bound in gen.where_clause.predicates {
|
2020-07-03 09:48:28 +00:00
|
|
|
if_chain! {
|
|
|
|
if let WherePredicate::BoundPredicate(ref p) = bound;
|
|
|
|
if p.bounds.len() as u64 <= self.max_trait_bounds;
|
2021-10-29 16:14:22 +00:00
|
|
|
if !p.span.from_expansion();
|
2022-01-04 19:20:27 +00:00
|
|
|
if let Some(ref v) = map.insert(
|
|
|
|
SpanlessTy { ty: p.bounded_ty, cx },
|
|
|
|
p.bounds.iter().collect::<Vec<_>>()
|
|
|
|
);
|
2020-07-03 09:48:28 +00:00
|
|
|
|
|
|
|
then {
|
2019-07-27 20:58:29 +00:00
|
|
|
let mut hint_string = format!(
|
|
|
|
"consider combining the bounds: `{}:",
|
|
|
|
snippet(cx, p.bounded_ty.span, "_")
|
|
|
|
);
|
2019-07-24 21:59:32 +00:00
|
|
|
for b in v.iter() {
|
2019-07-27 10:51:27 +00:00
|
|
|
if let GenericBound::Trait(ref poly_trait_ref, _) = b {
|
|
|
|
let path = &poly_trait_ref.trait_ref.path;
|
2020-03-27 14:34:29 +00:00
|
|
|
hint_string.push_str(&format!(
|
|
|
|
" {} +",
|
|
|
|
snippet_with_applicability(cx, path.span, "..", &mut applicability)
|
|
|
|
));
|
2019-07-27 10:51:27 +00:00
|
|
|
}
|
2019-02-15 20:21:13 +00:00
|
|
|
}
|
2019-07-24 21:59:32 +00:00
|
|
|
for b in p.bounds.iter() {
|
2019-07-27 10:51:27 +00:00
|
|
|
if let GenericBound::Trait(ref poly_trait_ref, _) = b {
|
|
|
|
let path = &poly_trait_ref.trait_ref.path;
|
2020-03-27 14:34:29 +00:00
|
|
|
hint_string.push_str(&format!(
|
|
|
|
" {} +",
|
|
|
|
snippet_with_applicability(cx, path.span, "..", &mut applicability)
|
|
|
|
));
|
2019-07-27 10:51:27 +00:00
|
|
|
}
|
2019-02-15 20:21:13 +00:00
|
|
|
}
|
|
|
|
hint_string.truncate(hint_string.len() - 2);
|
|
|
|
hint_string.push('`');
|
2020-01-27 01:56:22 +00:00
|
|
|
span_lint_and_help(
|
2019-02-15 20:21:13 +00:00
|
|
|
cx,
|
|
|
|
TYPE_REPETITION_IN_BOUNDS,
|
|
|
|
p.span,
|
|
|
|
"this type has already been used as a bound predicate",
|
2020-04-18 10:28:29 +00:00
|
|
|
None,
|
2019-02-15 20:21:13 +00:00
|
|
|
&hint_string,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-28 14:23:47 +00:00
|
|
|
|
|
|
|
fn check_trait_bound_duplication(cx: &LateContext<'_>, gen: &'_ Generics<'_>) {
|
2021-10-29 16:14:22 +00:00
|
|
|
if gen.span.from_expansion() || gen.params.is_empty() || gen.where_clause.predicates.is_empty() {
|
2020-07-28 14:23:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut map = FxHashMap::default();
|
|
|
|
for param in gen.params {
|
|
|
|
if let ParamName::Plain(ref ident) = param.name {
|
|
|
|
let res = param
|
|
|
|
.bounds
|
|
|
|
.iter()
|
|
|
|
.filter_map(get_trait_res_span_from_bound)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
map.insert(*ident, res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for predicate in gen.where_clause.predicates {
|
|
|
|
if_chain! {
|
|
|
|
if let WherePredicate::BoundPredicate(ref bound_predicate) = predicate;
|
2021-10-29 16:14:22 +00:00
|
|
|
if !bound_predicate.span.from_expansion();
|
2021-04-02 21:35:32 +00:00
|
|
|
if let TyKind::Path(QPath::Resolved(_, Path { segments, .. })) = bound_predicate.bounded_ty.kind;
|
2020-07-28 14:23:47 +00:00
|
|
|
if let Some(segment) = segments.first();
|
|
|
|
if let Some(trait_resolutions_direct) = map.get(&segment.ident);
|
|
|
|
then {
|
|
|
|
for (res_where, _) in bound_predicate.bounds.iter().filter_map(get_trait_res_span_from_bound) {
|
|
|
|
if let Some((_, span_direct)) = trait_resolutions_direct
|
|
|
|
.iter()
|
|
|
|
.find(|(res_direct, _)| *res_direct == res_where) {
|
|
|
|
span_lint_and_help(
|
|
|
|
cx,
|
|
|
|
TRAIT_DUPLICATION_IN_BOUNDS,
|
|
|
|
*span_direct,
|
|
|
|
"this trait bound is already specified in the where clause",
|
|
|
|
None,
|
|
|
|
"consider removing this trait bound",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|