Skip to content

Add lint_duplicate_trait for duplicate specified traits in a trait object #110991

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
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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 rustc_data_structures::fx::FxHashSet;

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; };

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

let mut seen_def_ids = FxHashSet::default();

for bound in bounds.iter() {
let Some(def_id) = bound.trait_ref.trait_def_id() else { continue; };

let already_seen = !seen_def_ids.insert(def_id);

if already_seen {
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,
},
)
}
}
}
}
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