-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Create unnecessary_send_constraint
lint for &(dyn ... + Send)
#110961
Changes from 17 commits
efbdc5b
532368c
6ed01d9
a00fd67
30af6f0
cd7bff1
cb9086b
cef8a0f
c16b474
ebf545f
657ec63
2321cb1
b914451
72adaec
63b0988
430e1db
8e02749
e9fe48c
b02fdd2
7083a31
df19546
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use rustc_span::sym; | ||
|
||
use crate::hir; | ||
|
||
use crate::{lints::UselessSendConstraintDiag, LateContext, LateLintPass}; | ||
|
||
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 unless they have a `Sync` bound, so constraining them to `Send` without `Sync` is useless. | ||
pub USELESS_SEND_CONSTRAINT, | ||
john-h-k marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Warn, | ||
"constraining a reference to `Send` without `Sync` 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; | ||
|
||
cx.tcx.emit_spanned_lint( | ||
USELESS_SEND_CONSTRAINT, | ||
send_bound.trait_ref.hir_ref_id, // is this correct? | ||
send_bound.span, | ||
UselessSendConstraintDiag { only_trait, suggestion: send_bound.span }, | ||
) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -695,7 +695,7 @@ impl Error { | |
#[stable(feature = "io_error_inner", since = "1.3.0")] | ||
#[must_use] | ||
#[inline] | ||
pub fn get_ref(&self) -> Option<&(dyn error::Error + Send + Sync + 'static)> { | ||
pub fn get_ref(&self) -> Option<&(dyn error::Error + Sync + 'static)> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, in any case, changing these methods is a breaking change the impact of which I’m not sure has been tested anywhere yet. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Oops, my bad. We cannot use |
||
match self.repr.data() { | ||
ErrorData::Os(..) => None, | ||
ErrorData::Simple(..) => None, | ||
|
@@ -769,7 +769,7 @@ impl Error { | |
#[stable(feature = "io_error_inner", since = "1.3.0")] | ||
#[must_use] | ||
#[inline] | ||
pub fn get_mut(&mut self) -> Option<&mut (dyn error::Error + Send + Sync + 'static)> { | ||
pub fn get_mut(&mut self) -> Option<&mut (dyn error::Error + Sync + 'static)> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a mutable reference. For |
||
match self.repr.data_mut() { | ||
ErrorData::Os(..) => None, | ||
ErrorData::Simple(..) => None, | ||
|
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)) {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replacing by
Any
is a very strange suggestion. Why wouldtype_id
machinery be necessary? Can't the'static
bound cause a problem?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't sure what else to put, this is for the scenario where
Send
is the only trait specified (so removing it would be invalid)