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

Create unnecessary_send_constraint lint for &(dyn ... + Send) #110961

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,16 @@ lint_redundant_semicolons =
*[false] this semicolon
}

lint_useless_send_constraint =
constraining a reference to `Send` is useless, consider {$only_trait ->
[true] replacing it with `Any`
*[false] removing it
}
.suggestion = {$only_trait ->
[true] replace this with `Any`
*[false] remove this
}

lint_drop_trait_constraints =
bounds on `{$predicate}` are most likely incorrect, consider instead using `{$needs_drop}` to detect whether a type can be trivially dropped

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
//! `late_lint_methods!` invocation in `lib.rs`.

use crate::fluent_generated as fluent;
use crate::lints::UselessSendConstraintDiag;
use crate::{
errors::BuiltinEllipsisInclusiveRangePatterns,
lints::{
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ late_lint_methods!(
OpaqueHiddenInferredBound: OpaqueHiddenInferredBound,
MultipleSupertraitUpcastable: MultipleSupertraitUpcastable,
MapUnitFn: MapUnitFn,
UselessSendConstraint: UselessSendConstraint,
]
]
);
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,15 @@ pub struct RedundantSemicolonsDiag {
pub suggestion: Span,
}

// lint_useless_send_constraint.rs
#[derive(LintDiagnostic)]
#[diag(lint_useless_send_constraint)]
pub struct UselessSendConstraintDiag {
pub only_trait: bool,
#[suggestion(code = "", applicability = "maybe-incorrect")]
pub suggestion: Span,
}

// traits.rs
pub struct DropTraitConstraintsDiag<'a> {
pub predicate: Predicate<'a>,
Expand Down
44 changes: 44 additions & 0 deletions compiler/rustc_lint/src/useless_send_constraint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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,
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, mutbl: _mutbl }) = ty.kind else { return; };

let 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 },
)
}
}
}
1 change: 1 addition & 0 deletions library/core/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2246,6 +2246,7 @@ impl<T: CoerceUnsized<U>, U> CoerceUnsized<SyncUnsafeCell<U>> for SyncUnsafeCell
impl<T: DispatchFromDyn<U>, U> DispatchFromDyn<SyncUnsafeCell<U>> for SyncUnsafeCell<T> {}

#[allow(unused)]
#[allow(useless_send_constraint)]
fn assert_coerce_unsized(
a: UnsafeCell<&i32>,
b: SyncUnsafeCell<&i32>,
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/lint/useless-send-constraint/useless-send-constraint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// check-pass
#![warn(useless_send_constraint)]

use std::any::Any;

fn main() {}

fn foo(_a: &(dyn Any + Send + Send)) {

}