-
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 all 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,57 @@ | ||
use rustc_span::sym; | ||
|
||
use crate::hir; | ||
|
||
use crate::{lints::UnnecessarySendConstraintDiag, LateContext, LateLintPass}; | ||
|
||
declare_lint! { | ||
/// The `unnecessary_send_constraint` lints unnecessary constraint of references to `Send`. | ||
/// | ||
/// ### Example | ||
/// | ||
/// ```rust,compile_fail | ||
/// #[deny(unnecessary_send_constraint)] | ||
/// fn foo(_: &(dyn Any + Send)) {} | ||
/// ``` | ||
/// | ||
/// {{produces}} | ||
/// | ||
/// ### Explanation | ||
/// | ||
/// Shared references cannot be sent across threads unless they have a `Sync` bound, so constraining them to `Send` without `Sync` is unnecessary. | ||
pub UNNECESSARY_SEND_CONSTRAINT, | ||
Warn, | ||
"constraining a reference to `Send` without `Sync` is unnecessary, consider removing it" | ||
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. Shouldn't the recommendation be to replace 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. See this comment: #110961 (comment) In those situations, it stems from a 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. That seems like a fairly special case to me, I would not expect this to be common enough to warrant a general recommendation of dropping the 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. So it should recommend replacing |
||
} | ||
|
||
declare_lint_pass!(UnnecessarySendConstraint => [UNNECESSARY_SEND_CONSTRAINT]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for UnnecessarySendConstraint { | ||
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( | ||
UNNECESSARY_SEND_CONSTRAINT, | ||
send_bound.trait_ref.hir_ref_id, // is this correct? | ||
send_bound.span, | ||
UnnecessarySendConstraintDiag { 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(unnecessary_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)