-
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
Changes from 14 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,58 @@ | ||
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 thay 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, ..), | ||
.. | ||
}, | ||
mutbl: hir::Mutability::Not, // only immutable references | ||
}, | ||
.. | ||
) = ty.kind else { return; }; | ||
|
||
let send = cx.tcx.get_diagnostic_item(sym::Send); | ||
let sync = cx.tcx.get_diagnostic_item(sym::Sync); | ||
|
||
let send_bound = bounds.iter().find(|b| b.trait_ref.trait_def_id() == send); | ||
let sync_bound = bounds.iter().find(|b| b.trait_ref.trait_def_id() == sync); | ||
|
||
if let Some(send_bound) = send_bound && sync_bound.is_none() { | ||
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, | ||
|
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)) {} |
Uh oh!
There was an error while loading. Please reload this page.