|
1 | 1 | use crate::LateContext; |
2 | 2 | use crate::LateLintPass; |
3 | 3 | use crate::LintContext; |
| 4 | +use hir::GenericBound; |
| 5 | +use rustc_data_structures::stable_set::FxHashSet; |
4 | 6 | use rustc_errors::fluent; |
5 | 7 | use rustc_hir as hir; |
6 | 8 | use rustc_span::symbol::sym; |
@@ -132,3 +134,44 @@ impl<'tcx> LateLintPass<'tcx> for DropTraitConstraints { |
132 | 134 | } |
133 | 135 | } |
134 | 136 | } |
| 137 | + |
| 138 | +declare_lint! { |
| 139 | + pub DUP_TRAIT_BOUNDS, |
| 140 | + Deny, |
| 141 | + "duplicate trait bounds" |
| 142 | +} |
| 143 | + |
| 144 | +declare_lint_pass!( |
| 145 | + DuplicateTraitBounds => [DUP_TRAIT_BOUNDS] |
| 146 | +); |
| 147 | + |
| 148 | +impl<'tcx> LateLintPass<'tcx> for DuplicateTraitBounds { |
| 149 | + fn check_item(&mut self, cx: &LateContext<'_>, item: &'tcx hir::Item<'tcx>) { |
| 150 | + let bounds: &[hir::GenericBound<'_>] = match &item.kind { |
| 151 | + hir::ItemKind::Trait(_, _, _, bounds, _) => bounds, |
| 152 | + hir::ItemKind::TraitAlias(_, bounds) => bounds, |
| 153 | + _ => return, |
| 154 | + }; |
| 155 | + |
| 156 | + let mut set = FxHashSet::default(); |
| 157 | + for bound in bounds.into_iter() { |
| 158 | + match bound { |
| 159 | + GenericBound::Trait(polytraitref, _) => { |
| 160 | + let Some(did) = polytraitref.trait_ref.trait_def_id() else { continue; }; |
| 161 | + // If inserting the trait bound into the set returns `false`, |
| 162 | + // there is a duplicate. |
| 163 | + if !set.insert(did) { |
| 164 | + let span = polytraitref.span; |
| 165 | + cx.struct_span_lint(DUP_TRAIT_BOUNDS, span, |lint| { |
| 166 | + let msg = format!("Duplicate trait bound"); |
| 167 | + lint.build(&msg) |
| 168 | + .span_help(span, "Remove this duplicate trait bound") |
| 169 | + .emit(); |
| 170 | + }); |
| 171 | + }; |
| 172 | + } |
| 173 | + _ => continue, |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments