From dfed2e31d5caa0ff7e53b442e8bd3585e78aba09 Mon Sep 17 00:00:00 2001 From: Yechan Bae Date: Fri, 1 Oct 2021 14:00:50 -0400 Subject: [PATCH] Do not use full type path in help message --- clippy_lints/src/non_send_field_in_send_ty.rs | 22 +++++------ tests/ui/non_send_field_in_send_ty.stderr | 38 +++++++++---------- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/clippy_lints/src/non_send_field_in_send_ty.rs b/clippy_lints/src/non_send_field_in_send_ty.rs index b43fb02d329..399b1a929e2 100644 --- a/clippy_lints/src/non_send_field_in_send_ty.rs +++ b/clippy_lints/src/non_send_field_in_send_ty.rs @@ -1,14 +1,14 @@ use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::is_lint_allowed; +use clippy_utils::source::snippet; use clippy_utils::ty::{implements_trait, is_copy}; use rustc_ast::ImplPolarity; use rustc_hir::def_id::DefId; -use rustc_hir::{Item, ItemKind}; +use rustc_hir::{FieldDef, Item, ItemKind, Node}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty::{self, subst::GenericArgKind, Ty}; use rustc_session::{declare_tool_lint, impl_lint_pass}; -use rustc_span::symbol::Symbol; -use rustc_span::{sym, Span}; +use rustc_span::sym; declare_clippy_lint! { /// ### What it does @@ -99,11 +99,10 @@ impl<'tcx> LateLintPass<'tcx> for NonSendFieldInSendTy { if !is_lint_allowed(cx, NON_SEND_FIELD_IN_SEND_TY, field_hir_id); if let field_ty = field.ty(cx.tcx, impl_trait_substs); if !ty_allowed_in_send(cx, field_ty, send_trait); - if let Some(field_span) = hir_map.span_if_local(field.did); + if let Node::Field(field_def) = hir_map.get(field_hir_id); then { non_send_fields.push(NonSendField { - name: hir_map.name(field_hir_id), - span: field_span, + def: field_def, ty: field_ty, generic_params: collect_generic_params(cx, field_ty), }) @@ -119,13 +118,13 @@ impl<'tcx> LateLintPass<'tcx> for NonSendFieldInSendTy { item.span, &format!( "this implementation is unsound, as some fields in `{}` are `!Send`", - self_ty + snippet(cx, hir_impl.self_ty.span, "Unknown") ), |diag| { for field in non_send_fields { diag.span_note( - field.span, - &format!("the field `{}` has type `{}` which is `!Send`", field.name, field.ty), + field.def.span, + &format!("the type of field `{}` is `!Send`", field.def.ident.name), ); match field.generic_params.len() { @@ -135,7 +134,7 @@ impl<'tcx> LateLintPass<'tcx> for NonSendFieldInSendTy { "add bounds on type parameter{} `{}` that satisfy `{}: Send`", if field.generic_params.len() > 1 { "s" } else { "" }, field.generic_params_string(), - field.ty + snippet(cx, field.def.ty.span, "Unknown"), )), }; } @@ -148,8 +147,7 @@ impl<'tcx> LateLintPass<'tcx> for NonSendFieldInSendTy { } struct NonSendField<'tcx> { - name: Symbol, - span: Span, + def: &'tcx FieldDef<'tcx>, ty: Ty<'tcx>, generic_params: Vec>, } diff --git a/tests/ui/non_send_field_in_send_ty.stderr b/tests/ui/non_send_field_in_send_ty.stderr index 6c7312d0152..f49a7355b53 100644 --- a/tests/ui/non_send_field_in_send_ty.stderr +++ b/tests/ui/non_send_field_in_send_ty.stderr @@ -5,12 +5,12 @@ LL | unsafe impl Send for RingBuffer {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `-D clippy::non-send-field-in-send-ty` implied by `-D warnings` -note: the field `data` has type `std::vec::Vec>` which is `!Send` +note: the type of field `data` is `!Send` --> $DIR/non_send_field_in_send_ty.rs:11:5 | LL | data: Vec>, | ^^^^^^^^^^^^^^^^^^^^^^^^ - = help: add bounds on type parameter `T` that satisfy `std::vec::Vec>: Send` + = help: add bounds on type parameter `T` that satisfy `Vec>: Send` error: this implementation is unsound, as some fields in `MvccRwLock` are `!Send` --> $DIR/non_send_field_in_send_ty.rs:24:1 @@ -18,12 +18,12 @@ error: this implementation is unsound, as some fields in `MvccRwLock` are `!S LL | unsafe impl Send for MvccRwLock {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | -note: the field `lock` has type `std::sync::Mutex>` which is `!Send` +note: the type of field `lock` is `!Send` --> $DIR/non_send_field_in_send_ty.rs:21:5 | LL | lock: Mutex>, | ^^^^^^^^^^^^^^^^^^^ - = help: add bounds on type parameter `T` that satisfy `std::sync::Mutex>: Send` + = help: add bounds on type parameter `T` that satisfy `Mutex>: Send` error: this implementation is unsound, as some fields in `ArcGuard` are `!Send` --> $DIR/non_send_field_in_send_ty.rs:32:1 @@ -31,12 +31,12 @@ error: this implementation is unsound, as some fields in `ArcGuard` are ` LL | unsafe impl Send for ArcGuard {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | -note: the field `head` has type `std::sync::Arc` which is `!Send` +note: the type of field `head` is `!Send` --> $DIR/non_send_field_in_send_ty.rs:29:5 | LL | head: Arc, | ^^^^^^^^^^^^^ - = help: add bounds on type parameter `RC` that satisfy `std::sync::Arc: Send` + = help: add bounds on type parameter `RC` that satisfy `Arc: Send` error: this implementation is unsound, as some fields in `DeviceHandle` are `!Send` --> $DIR/non_send_field_in_send_ty.rs:48:1 @@ -44,7 +44,7 @@ error: this implementation is unsound, as some fields in `DeviceHandle` are ` LL | unsafe impl Send for DeviceHandle {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | -note: the field `context` has type `T` which is `!Send` +note: the type of field `context` is `!Send` --> $DIR/non_send_field_in_send_ty.rs:44:5 | LL | context: T, @@ -57,7 +57,7 @@ error: this implementation is unsound, as some fields in `NoGeneric` are `!Send` LL | unsafe impl Send for NoGeneric {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | -note: the field `rc_is_not_send` has type `std::rc::Rc` which is `!Send` +note: the type of field `rc_is_not_send` is `!Send` --> $DIR/non_send_field_in_send_ty.rs:52:5 | LL | rc_is_not_send: Rc, @@ -70,19 +70,19 @@ error: this implementation is unsound, as some fields in `MultiField` are `!S LL | unsafe impl Send for MultiField {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | -note: the field `field1` has type `T` which is `!Send` +note: the type of field `field1` is `!Send` --> $DIR/non_send_field_in_send_ty.rs:58:5 | LL | field1: T, | ^^^^^^^^^ = help: add `T: Send` bound in `Send` impl -note: the field `field2` has type `T` which is `!Send` +note: the type of field `field2` is `!Send` --> $DIR/non_send_field_in_send_ty.rs:59:5 | LL | field2: T, | ^^^^^^^^^ = help: add `T: Send` bound in `Send` impl -note: the field `field3` has type `T` which is `!Send` +note: the type of field `field3` is `!Send` --> $DIR/non_send_field_in_send_ty.rs:60:5 | LL | field3: T, @@ -95,7 +95,7 @@ error: this implementation is unsound, as some fields in `MyOption` are `!Sen LL | unsafe impl Send for MyOption {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | -note: the field `0` has type `T` which is `!Send` +note: the type of field `0` is `!Send` --> $DIR/non_send_field_in_send_ty.rs:66:12 | LL | MySome(T), @@ -108,12 +108,12 @@ error: this implementation is unsound, as some fields in `MultiParam` are LL | unsafe impl Send for MultiParam {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | -note: the field `vec` has type `std::vec::Vec<(A, B)>` which is `!Send` +note: the type of field `vec` is `!Send` --> $DIR/non_send_field_in_send_ty.rs:74:5 | LL | vec: Vec<(A, B)>, | ^^^^^^^^^^^^^^^^ - = help: add bounds on type parameters `A, B` that satisfy `std::vec::Vec<(A, B)>: Send` + = help: add bounds on type parameters `A, B` that satisfy `Vec<(A, B)>: Send` error: this implementation is unsound, as some fields in `HeuristicTest` are `!Send` --> $DIR/non_send_field_in_send_ty.rs:95:1 @@ -121,7 +121,7 @@ error: this implementation is unsound, as some fields in `HeuristicTest` are `!S LL | unsafe impl Send for HeuristicTest {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | -note: the field `field4` has type `(*const NonSend, std::rc::Rc)` which is `!Send` +note: the type of field `field4` is `!Send` --> $DIR/non_send_field_in_send_ty.rs:90:5 | LL | field4: (*const NonSend, Rc), @@ -134,7 +134,7 @@ error: this implementation is unsound, as some fields in `AttrTest3` are `!Se LL | unsafe impl Send for AttrTest3 {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | -note: the field `0` has type `T` which is `!Send` +note: the type of field `0` is `!Send` --> $DIR/non_send_field_in_send_ty.rs:109:11 | LL | Enum2(T), @@ -147,20 +147,20 @@ error: this implementation is unsound, as some fields in `Complex` are ` LL | unsafe impl

Send for Complex {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | -note: the field `field1` has type `P` which is `!Send` +note: the type of field `field1` is `!Send` --> $DIR/non_send_field_in_send_ty.rs:118:5 | LL | field1: A, | ^^^^^^^^^ = help: add `P: Send` bound in `Send` impl -error: this implementation is unsound, as some fields in `Complex>` are `!Send` +error: this implementation is unsound, as some fields in `Complex>` are `!Send` --> $DIR/non_send_field_in_send_ty.rs:125:1 | LL | unsafe impl Send for Complex> {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | -note: the field `field2` has type `std::sync::MutexGuard<'static, bool>` which is `!Send` +note: the type of field `field2` is `!Send` --> $DIR/non_send_field_in_send_ty.rs:119:5 | LL | field2: B,