-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Create unnecessary_send_constraint
lint for &(dyn ... + Send)
#110961
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
Closed
Changes from 7 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
efbdc5b
Initial impl of `lint_useless_send_constraint`
john-h-k 532368c
Fixes
john-h-k 6ed01d9
Trait test and edge case handling
john-h-k a00fd67
fmt
john-h-k 30af6f0
WIP tests
john-h-k cd7bff1
Comment round #1
john-h-k cb9086b
Comment round #1
john-h-k cef8a0f
Remove comment
john-h-k c16b474
Compiling except for the actual lint
john-h-k ebf545f
Remove comment
john-h-k 657ec63
Remove uses of send
john-h-k 2321cb1
Comments
john-h-k b914451
Style
john-h-k 72adaec
Fix sync behaviour
john-h-k 63b0988
Update compiler/rustc_lint/src/useless_send_constraint.rs
john-h-k 430e1db
Voila
john-h-k 8e02749
Update useless_send_constraint.rs
john-h-k e9fe48c
Comments
john-h-k b02fdd2
fmt
john-h-k 7083a31
renaming
john-h-k df19546
Address review comments
john-h-k File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
declare_lint! { | ||
/// The `lint_useless_send_constraint` lints useless constraint of references to `Send`. | ||
/// | ||
/// ### Example | ||
/// | ||
/// ```rust,compile_fail | ||
/// fn foo(_: &(dyn Any + Send>) {} | ||
/// ``` | ||
/// | ||
/// {{produces}} | ||
/// | ||
/// ### Explanation | ||
/// | ||
/// References cannot be sent across threads, so constraining them to `Send` is useless. | ||
pub USELESS_SEND_CONSTRAINT, | ||
john-h-k marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Warn, | ||
"constraining a reference to `Send` is useless, consider removing it" | ||
} | ||
|
||
declare_lint_pass!(UselessSendConstraint => [USELESS_SEND_CONSTRAINT]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for UselessSendConstraint { | ||
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 send = cx.tcx.get_diagnostic_item(sym::Send); | ||
|
||
let send_bound = bounds.iter().find(|b| b.trait_ref.trait_def_id() == send); | ||
|
||
if let Some(send_bound) = send_bound { | ||
let only_trait = bounds.len() == 1; | ||
|
||
// We have multiple bounds. one is `Send` | ||
// Suggest removing it | ||
cx.emit_spanned_lint( | ||
USELESS_SEND_CONSTRAINT, | ||
send_bound.span, | ||
UselessSendConstraintDiag { only_trait, suggestion: send_bound.span }, | ||
) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
tests/ui/lint/useless-send-constraint/useless-send-constraint.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#![warn(useless_send_constraint)] | ||
|
||
use std::any::Any; | ||
|
||
fn main() {} | ||
|
||
fn fine(_a: &dyn Any) {} | ||
|
||
fn should_replace_with_any(_a: &(dyn Send)) {} | ||
|
||
fn should_remove_send(_a: &(dyn Any + Send)) {} | ||
|
||
fn should_remove_send_duplicate(_a: &(dyn Any + Send)) {} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.