Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ lint_redundant_semicolons =
*[false] this semicolon
}

lint_duplicate_trait =
trait `{$trait_name}` was already specified
.suggestion = remove duplicate trait

lint_drop_trait_constraints =
bounds on `{$predicate}` are most likely incorrect, consider instead using `{$needs_drop}` to detect whether a type can be trivially dropped

Expand Down
65 changes: 65 additions & 0 deletions compiler/rustc_lint/src/duplicate_trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use crate::hir;

use crate::{lints::DuplicateTraitDiag, LateContext, LateLintPass};

declare_lint! {
/// The `lint_duplicate_trait` lints repetition of traits.
///
/// ### Example
///
/// ```rust,compile_fail
/// fn foo(_: &(dyn MyTrait + Send + Send>) {}
/// ```
///
/// {{produces}}
///
/// ### Explanation
///
/// Duplicate trait `Send` in trait object.
pub DUPLICATE_TRAIT,
Warn,
"duplicate trait constraint in trait object"
}

declare_lint_pass!(DuplicateTrait => [DUPLICATE_TRAIT]);

impl<'tcx> LateLintPass<'tcx> for DuplicateTrait {
fn check_ty(&mut self, cx: &LateContext<'tcx>, ty: &'tcx hir::Ty<'tcx>) {
let hir::TyKind::Ref(
..,
hir::MutTy {
ty: hir::Ty {
kind: hir::TyKind::TraitObject(bounds, ..),
..
},
..
}
) = ty.kind else { return; };

let mut bounds = (*bounds).to_owned();

bounds.sort_unstable_by_key(|b| b.trait_ref.trait_def_id());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DefIds are not stable across compilations and should not be used for sorting like here. Instead of what you're doing right now, you should use a FxHashSet and iterate through the bounds (in source order) and insert their def ids into the set. If a def id is already in the set, emit the lint.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will make that change. Out of curiosity, why does the fact they're unstable across compilations matter? The only aim of the sort is to group like DefIds together, which should still be valid given it is within one compilation right?


if bounds.len() < 2 {
return;
}

let mut last_bound = &bounds[0];
for bound in bounds.iter().skip(1) {
if last_bound.trait_ref.trait_def_id() == bound.trait_ref.trait_def_id()
&& let Some(def_id) = bound.trait_ref.trait_def_id() {
cx.tcx.emit_spanned_lint(
DUPLICATE_TRAIT,
bound.trait_ref.hir_ref_id, // is this correct?
bound.span,
DuplicateTraitDiag {
trait_name: cx.tcx.item_name(def_id),
suggestion: bound.span
},
)
}

last_bound = bound;
}
}
}
3 changes: 3 additions & 0 deletions compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ mod array_into_iter;
pub mod builtin;
mod context;
mod deref_into_dyn_supertrait;
mod duplicate_trait;
mod early;
mod enum_intrinsics_non_enums;
mod errors;
Expand Down Expand Up @@ -119,6 +120,7 @@ use unused::*;
pub use builtin::SoftLints;
pub use context::{CheckLintNameResult, FindLintError, LintStore};
pub use context::{EarlyContext, LateContext, LintContext};
use duplicate_trait::DuplicateTrait;
pub use early::{check_ast_node, EarlyCheckNode};
pub use late::{check_crate, unerased_lint_store};
pub use passes::{EarlyLintPass, LateLintPass};
Expand Down Expand Up @@ -242,6 +244,7 @@ late_lint_methods!(
OpaqueHiddenInferredBound: OpaqueHiddenInferredBound,
MultipleSupertraitUpcastable: MultipleSupertraitUpcastable,
MapUnitFn: MapUnitFn,
DuplicateTrait: DuplicateTrait,
]
]
);
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,15 @@ pub struct RedundantSemicolonsDiag {
pub suggestion: Span,
}

// duplicate_trait.rs
#[derive(LintDiagnostic)]
#[diag(lint_duplicate_trait)]
pub struct DuplicateTraitDiag {
pub trait_name: Symbol,
#[suggestion(code = "", applicability = "maybe-incorrect")]
pub suggestion: Span,
}

// traits.rs
pub struct DropTraitConstraintsDiag<'a> {
pub predicate: Predicate<'a>,
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/lint/duplicate-trait/duplicate-trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// check-fail
#![warn(duplicate_trait)]

use std::any::Any;

fn main() {}

fn fine(_a: &(dyn Any + Send)) {}

fn duplicate_once(_a: &(dyn Any + Send + Send)) {} //~WARNING duplicate trait

fn duplicate_twice(_a: &(dyn Any + Send + Send + Send)) {} //~WARNING duplicate trait

fn duplicate_out_of_order(_a: &(dyn Any + Send + Sync + Send)) {} //~WARNING duplicate trait

fn duplicate_multiple(_a: &(dyn Any + Send + Sync + Send + Sync)) {} //~WARNING duplicate trait
43 changes: 43 additions & 0 deletions tests/ui/lint/duplicate-trait/duplicate-trait.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
warning: trait `Send` was already specified
--> $DIR/duplicate-trait.rs:9:42
|
LL | fn duplicate_once(_a: &(dyn Any + Send + Send)) {}
| ^^^^ help: remove duplicate trait
|
note: the lint level is defined here
--> $DIR/duplicate-trait.rs:1:9
|
LL | #![warn(duplicate_trait)]
| ^^^^^^^^^^^^^^^

warning: trait `Send` was already specified
--> $DIR/duplicate-trait.rs:11:43
|
LL | fn duplicate_twice(_a: &(dyn Any + Send + Send + Send)) {}
| ^^^^ help: remove duplicate trait

warning: trait `Send` was already specified
--> $DIR/duplicate-trait.rs:11:50
|
LL | fn duplicate_twice(_a: &(dyn Any + Send + Send + Send)) {}
| ^^^^ help: remove duplicate trait

warning: trait `Send` was already specified
--> $DIR/duplicate-trait.rs:13:57
|
LL | fn duplicate_out_of_order(_a: &(dyn Any + Send + Sync + Send)) {}
| ^^^^ help: remove duplicate trait

warning: trait `Send` was already specified
--> $DIR/duplicate-trait.rs:15:53
|
LL | fn duplicate_multiple(_a: &(dyn Any + Send + Sync + Send + Sync)) {}
| ^^^^ help: remove duplicate trait

warning: trait `Sync` was already specified
--> $DIR/duplicate-trait.rs:15:60
|
LL | fn duplicate_multiple(_a: &(dyn Any + Send + Sync + Send + Sync)) {}
| ^^^^ help: remove duplicate trait

warning: 6 warnings emitted