Skip to content
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

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

Closed
wants to merge 5 commits into from

Conversation

john-h-k
Copy link
Contributor

This adds a new lint, lint_duplicate_traits, which warns when traits are duplicately specified, which currently produces no diagnostics.

fn duplicate(_a: &(dyn Trait + Send + Send)) {} // Currently generates no warnings, but now will

Related to #110961

@rustbot
Copy link
Collaborator

rustbot commented Apr 29, 2023

r? @lcnr

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 29, 2023
@rust-log-analyzer

This comment has been minimized.


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?

@klensy
Copy link
Contributor

klensy commented Apr 29, 2023

Shouldn't https://rust-lang.github.io/rust-clippy/master/#trait_duplication_in_bounds be extended instead?

@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-llvm-14 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
..............

failures:

---- [ui] tests/ui/lint/duplicate-trait/duplicate-trait.rs stdout ----
error: ui test compiled successfully!
status: exit status: 0
status: exit status: 0
command: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/lint/duplicate-trait/duplicate-trait.rs" "-Zthreads=1" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Zdeduplicate-diagnostics=no" "-Cstrip=debuginfo" "--remap-path-prefix=/checkout/tests/ui=fake-test-src-base" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/lint/duplicate-trait/duplicate-trait" "-A" "unused" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/lint/duplicate-trait/duplicate-trait/auxiliary"
stdout: none
warning: trait `Send` was already specified
warning: trait `Send` was already specified
  --> fake-test-src-base/lint/duplicate-trait/duplicate-trait.rs:10:42
   |
LL | fn duplicate_once(_a: &(dyn Any + Send + Send)) {} //~WARNING duplicate trait
   |                                          ^^^^ help: remove duplicate trait
note: the lint level is defined here
note: the lint level is defined here
  --> fake-test-src-base/lint/duplicate-trait/duplicate-trait.rs:2:9
   |
LL | #![warn(duplicate_trait)]

warning: trait `Send` was already specified
warning: trait `Send` was already specified
  --> fake-test-src-base/lint/duplicate-trait/duplicate-trait.rs:12:43
   |
LL | fn duplicate_twice(_a: &(dyn Any + Send + Send + Send)) {} //~WARNING duplicate trait
   |                                           ^^^^ help: remove duplicate trait
warning: trait `Send` was already specified
warning: trait `Send` was already specified
  --> fake-test-src-base/lint/duplicate-trait/duplicate-trait.rs:12:50
   |
LL | fn duplicate_twice(_a: &(dyn Any + Send + Send + Send)) {} //~WARNING duplicate trait
   |                                                  ^^^^ help: remove duplicate trait
warning: trait `Send` was already specified
warning: trait `Send` was already specified
  --> fake-test-src-base/lint/duplicate-trait/duplicate-trait.rs:14:57
   |
LL | fn duplicate_out_of_order(_a: &(dyn Any + Send + Sync + Send)) {} //~WARNING duplicate trait
   |                                                         ^^^^ help: remove duplicate trait
warning: trait `Send` was already specified
warning: trait `Send` was already specified
  --> fake-test-src-base/lint/duplicate-trait/duplicate-trait.rs:16:53
   |
LL | fn duplicate_multiple(_a: &(dyn Any + Send + Sync + Send + Sync)) {} //~WARNING duplicate trait
   |                                                     ^^^^ help: remove duplicate trait
warning: trait `Sync` was already specified
warning: trait `Sync` was already specified
  --> fake-test-src-base/lint/duplicate-trait/duplicate-trait.rs:16:60
   |
LL | fn duplicate_multiple(_a: &(dyn Any + Send + Sync + Send + Sync)) {} //~WARNING duplicate trait
   |                                                            ^^^^ help: remove duplicate trait
warning: 6 warnings emitted
------------------------------------------


@john-h-k
Copy link
Contributor Author

Shouldn't https://rust-lang.github.io/rust-clippy/master/#trait_duplication_in_bounds be extended instead?

Thanks, have moved to rust-lang/rust-clippy#10727

@john-h-k john-h-k closed this Apr 30, 2023
bors added a commit to rust-lang/rust-clippy that referenced this pull request May 9, 2023
Extend `trait_duplication_in_bounds` to cover trait objects

This PR extends `trait_duplication_in_bounds` to cover trait objects.

Currently,
```rs
fn foo(_a: &(dyn Any + Send + Send)) {}
```

generates no warnings. With this PR, it will complain about a duplicate trait and can remove it

Moved from rust-lang/rust#110991

changelog: [`trait_duplication_in_bounds`]: warn on duplicate trait object constraints
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants