]> git.lizzy.rs Git - rust.git/commitdiff
Add ui-test for enable-raw-pointer-heuristic-for-send config
authorYechan Bae <yechan@gatech.edu>
Fri, 1 Oct 2021 17:07:25 +0000 (13:07 -0400)
committerYechan Bae <yechan@gatech.edu>
Fri, 1 Oct 2021 18:04:20 +0000 (14:04 -0400)
clippy_lints/src/non_send_field_in_send_ty.rs
tests/ui-toml/strict_non_send_field_in_send_ty/clippy.toml [new file with mode: 0644]
tests/ui-toml/strict_non_send_field_in_send_ty/test.rs [new file with mode: 0644]
tests/ui-toml/strict_non_send_field_in_send_ty/test.stderr [new file with mode: 0644]
tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr

index 83f6f786d26e329a3a6b76c76ca1a0af0d52e166..b43fb02d329353a114e93c083271651fc5b1b98a 100644 (file)
@@ -68,7 +68,7 @@ fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
         let ty_allowed_in_send = if self.enable_raw_pointer_heuristic {
             ty_allowed_with_raw_pointer_heuristic
         } else {
-            ty_implements_send_or_copy
+            ty_allowed_without_raw_pointer_heuristic
         };
 
         // Checks if we are in `Send` impl item.
@@ -176,14 +176,22 @@ fn collect_generic_params<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Vec<Ty<
         .collect()
 }
 
-/// Determine if the given type is `Send` or `Copy`
-fn ty_implements_send_or_copy(cx: &LateContext<'tcx>, ty: Ty<'tcx>, send_trait: DefId) -> bool {
-    implements_trait(cx, ty, send_trait, &[]) || is_copy(cx, ty)
+/// Be more strict when the heuristic is disabled
+fn ty_allowed_without_raw_pointer_heuristic<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, send_trait: DefId) -> bool {
+    if implements_trait(cx, ty, send_trait, &[]) {
+        return true;
+    }
+
+    if is_copy(cx, ty) && !contains_raw_pointer(cx, ty) {
+        return true;
+    }
+
+    false
 }
 
 /// Heuristic to allow cases like `Vec<*const u8>`
 fn ty_allowed_with_raw_pointer_heuristic<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, send_trait: DefId) -> bool {
-    if ty_implements_send_or_copy(cx, ty, send_trait) {
+    if implements_trait(cx, ty, send_trait, &[]) || is_copy(cx, ty) {
         return true;
     }
 
diff --git a/tests/ui-toml/strict_non_send_field_in_send_ty/clippy.toml b/tests/ui-toml/strict_non_send_field_in_send_ty/clippy.toml
new file mode 100644 (file)
index 0000000..a942709
--- /dev/null
@@ -0,0 +1 @@
+enable-raw-pointer-heuristic-for-send = false
diff --git a/tests/ui-toml/strict_non_send_field_in_send_ty/test.rs b/tests/ui-toml/strict_non_send_field_in_send_ty/test.rs
new file mode 100644 (file)
index 0000000..6306c9c
--- /dev/null
@@ -0,0 +1,43 @@
+#![warn(clippy::non_send_field_in_send_ty)]
+#![feature(extern_types)]
+
+use std::rc::Rc;
+
+// Basic tests should not be affected
+pub struct NoGeneric {
+    rc_is_not_send: Rc<String>,
+}
+
+unsafe impl Send for NoGeneric {}
+
+pub struct MultiField<T> {
+    field1: T,
+    field2: T,
+    field3: T,
+}
+
+unsafe impl<T> Send for MultiField<T> {}
+
+pub enum MyOption<T> {
+    MySome(T),
+    MyNone,
+}
+
+unsafe impl<T> Send for MyOption<T> {}
+
+// All fields are disallowed when raw pointer heuristic is off
+extern "C" {
+    type NonSend;
+}
+
+pub struct HeuristicTest {
+    field1: Vec<*const NonSend>,
+    field2: [*const NonSend; 3],
+    field3: (*const NonSend, *const NonSend, *const NonSend),
+    field4: (*const NonSend, Rc<u8>),
+    field5: Vec<Vec<*const NonSend>>,
+}
+
+unsafe impl Send for HeuristicTest {}
+
+fn main() {}
diff --git a/tests/ui-toml/strict_non_send_field_in_send_ty/test.stderr b/tests/ui-toml/strict_non_send_field_in_send_ty/test.stderr
new file mode 100644 (file)
index 0000000..093c932
--- /dev/null
@@ -0,0 +1,91 @@
+error: this implementation is unsound, as some fields in `NoGeneric` are `!Send`
+  --> $DIR/test.rs:11:1
+   |
+LL | unsafe impl Send for NoGeneric {}
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+   = note: `-D clippy::non-send-field-in-send-ty` implied by `-D warnings`
+note: the field `rc_is_not_send` has type `std::rc::Rc<std::string::String>` which is `!Send`
+  --> $DIR/test.rs:8:5
+   |
+LL |     rc_is_not_send: Rc<String>,
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^
+   = help: use a thread-safe type that implements `Send`
+
+error: this implementation is unsound, as some fields in `MultiField<T>` are `!Send`
+  --> $DIR/test.rs:19:1
+   |
+LL | unsafe impl<T> Send for MultiField<T> {}
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+note: the field `field1` has type `T` which is `!Send`
+  --> $DIR/test.rs:14:5
+   |
+LL |     field1: T,
+   |     ^^^^^^^^^
+   = help: add `T: Send` bound in `Send` impl
+note: the field `field2` has type `T` which is `!Send`
+  --> $DIR/test.rs:15:5
+   |
+LL |     field2: T,
+   |     ^^^^^^^^^
+   = help: add `T: Send` bound in `Send` impl
+note: the field `field3` has type `T` which is `!Send`
+  --> $DIR/test.rs:16:5
+   |
+LL |     field3: T,
+   |     ^^^^^^^^^
+   = help: add `T: Send` bound in `Send` impl
+
+error: this implementation is unsound, as some fields in `MyOption<T>` are `!Send`
+  --> $DIR/test.rs:26:1
+   |
+LL | unsafe impl<T> Send for MyOption<T> {}
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+note: the field `0` has type `T` which is `!Send`
+  --> $DIR/test.rs:22:12
+   |
+LL |     MySome(T),
+   |            ^
+   = help: add `T: Send` bound in `Send` impl
+
+error: this implementation is unsound, as some fields in `HeuristicTest` are `!Send`
+  --> $DIR/test.rs:41:1
+   |
+LL | unsafe impl Send for HeuristicTest {}
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+note: the field `field1` has type `std::vec::Vec<*const NonSend>` which is `!Send`
+  --> $DIR/test.rs:34:5
+   |
+LL |     field1: Vec<*const NonSend>,
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   = help: use a thread-safe type that implements `Send`
+note: the field `field2` has type `[*const NonSend; 3]` which is `!Send`
+  --> $DIR/test.rs:35:5
+   |
+LL |     field2: [*const NonSend; 3],
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   = help: use a thread-safe type that implements `Send`
+note: the field `field3` has type `(*const NonSend, *const NonSend, *const NonSend)` which is `!Send`
+  --> $DIR/test.rs:36:5
+   |
+LL |     field3: (*const NonSend, *const NonSend, *const NonSend),
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   = help: use a thread-safe type that implements `Send`
+note: the field `field4` has type `(*const NonSend, std::rc::Rc<u8>)` which is `!Send`
+  --> $DIR/test.rs:37:5
+   |
+LL |     field4: (*const NonSend, Rc<u8>),
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   = help: use a thread-safe type that implements `Send`
+note: the field `field5` has type `std::vec::Vec<std::vec::Vec<*const NonSend>>` which is `!Send`
+  --> $DIR/test.rs:38:5
+   |
+LL |     field5: Vec<Vec<*const NonSend>>,
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   = help: use a thread-safe type that implements `Send`
+
+error: aborting due to 4 previous errors
+
index 0eff2be796048fe2520ef0867cb67291bb2363dc..97bab1308aa52b12d94e269117263ef330ab1d0d 100644 (file)
@@ -1,4 +1,4 @@
-error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `avoid-breaking-exported-api`, `msrv`, `blacklisted-names`, `cognitive-complexity-threshold`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `pass-by-value-size-limit`, `too-many-lines-threshold`, `array-size-threshold`, `vec-box-size-threshold`, `max-trait-bounds`, `max-struct-bools`, `max-fn-params-bools`, `warn-on-all-wildcard-imports`, `disallowed-methods`, `disallowed-types`, `unreadable-literal-lint-fractions`, `upper-case-acronyms-aggressive`, `cargo-ignore-publish`, `standard-macro-braces`, `enforced-import-renames`, `allowed-scripts`, `enable-raw-pointer-heuristic`, `third-party` at line 5 column 1
+error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `avoid-breaking-exported-api`, `msrv`, `blacklisted-names`, `cognitive-complexity-threshold`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `pass-by-value-size-limit`, `too-many-lines-threshold`, `array-size-threshold`, `vec-box-size-threshold`, `max-trait-bounds`, `max-struct-bools`, `max-fn-params-bools`, `warn-on-all-wildcard-imports`, `disallowed-methods`, `disallowed-types`, `unreadable-literal-lint-fractions`, `upper-case-acronyms-aggressive`, `cargo-ignore-publish`, `standard-macro-braces`, `enforced-import-renames`, `allowed-scripts`, `enable-raw-pointer-heuristic-for-send`, `third-party` at line 5 column 1
 
 error: aborting due to previous error