Uplift clippy::drop_copy to rustc
This commit is contained in:
parent
3d1940d02e
commit
156f5563c7
5 changed files with 248 additions and 2 deletions
|
@ -523,3 +523,6 @@ lint_opaque_hidden_inferred_bound_sugg = add this bound
|
||||||
|
|
||||||
lint_drop_ref = calls to `std::mem::drop` with a reference instead of an owned value
|
lint_drop_ref = calls to `std::mem::drop` with a reference instead of an owned value
|
||||||
.note = argument has type `{$arg_ty}`
|
.note = argument has type `{$arg_ty}`
|
||||||
|
|
||||||
|
lint_drop_copy = calls to `std::mem::drop` with a value that implements `Copy`.
|
||||||
|
.note = argument has type `{$arg_ty}`
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use rustc_hir::{Arm, Expr, ExprKind, Node};
|
use rustc_hir::{Arm, Expr, ExprKind, Node};
|
||||||
use rustc_span::sym;
|
use rustc_span::sym;
|
||||||
|
|
||||||
use crate::{lints::DropRefDiag, LateContext, LateLintPass, LintContext};
|
use crate::{lints::{DropRefDiag, DropCopyDiag}, LateContext, LateLintPass, LintContext};
|
||||||
|
|
||||||
declare_lint! {
|
declare_lint! {
|
||||||
/// The `drop_ref` lint checks for calls to `std::mem::drop` with a reference
|
/// The `drop_ref` lint checks for calls to `std::mem::drop` with a reference
|
||||||
|
@ -31,7 +31,31 @@ declare_lint! {
|
||||||
"calls to `std::mem::drop` with a reference instead of an owned value"
|
"calls to `std::mem::drop` with a reference instead of an owned value"
|
||||||
}
|
}
|
||||||
|
|
||||||
declare_lint_pass!(DropForgetUseless => [DROP_REF]);
|
declare_lint! {
|
||||||
|
/// The `drop_copy` lint checks for calls to `std::mem::drop` with a value
|
||||||
|
/// that derives the Copy trait.
|
||||||
|
///
|
||||||
|
/// ### Example
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// let x: i32 = 42; // i32 implements Copy
|
||||||
|
/// std::mem::drop(x); // A copy of x is passed to the function, leaving the
|
||||||
|
/// // original unaffected
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// {{produces}}
|
||||||
|
///
|
||||||
|
/// ### Explanation
|
||||||
|
///
|
||||||
|
/// Calling `std::mem::drop` [does nothing for types that
|
||||||
|
/// implement Copy](https://doc.rust-lang.org/std/mem/fn.drop.html), since the
|
||||||
|
/// value will be copied and moved into the function on invocation.
|
||||||
|
pub DROP_COPY,
|
||||||
|
Warn,
|
||||||
|
"calls to `std::mem::drop` with a value that implements Copy"
|
||||||
|
}
|
||||||
|
|
||||||
|
declare_lint_pass!(DropForgetUseless => [DROP_REF, DROP_COPY]);
|
||||||
|
|
||||||
impl<'tcx> LateLintPass<'tcx> for DropForgetUseless {
|
impl<'tcx> LateLintPass<'tcx> for DropForgetUseless {
|
||||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
|
||||||
|
@ -41,11 +65,15 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetUseless {
|
||||||
&& let Some(fn_name) = cx.tcx.get_diagnostic_name(def_id)
|
&& let Some(fn_name) = cx.tcx.get_diagnostic_name(def_id)
|
||||||
{
|
{
|
||||||
let arg_ty = cx.typeck_results().expr_ty(arg);
|
let arg_ty = cx.typeck_results().expr_ty(arg);
|
||||||
|
let is_copy = arg_ty.is_copy_modulo_regions(cx.tcx, cx.param_env);
|
||||||
let drop_is_single_call_in_arm = is_single_call_in_arm(cx, arg, expr);
|
let drop_is_single_call_in_arm = is_single_call_in_arm(cx, arg, expr);
|
||||||
match fn_name {
|
match fn_name {
|
||||||
sym::mem_drop if arg_ty.is_ref() && !drop_is_single_call_in_arm => {
|
sym::mem_drop if arg_ty.is_ref() && !drop_is_single_call_in_arm => {
|
||||||
cx.emit_spanned_lint(DROP_REF, expr.span, DropRefDiag { arg_ty, note: arg.span });
|
cx.emit_spanned_lint(DROP_REF, expr.span, DropRefDiag { arg_ty, note: arg.span });
|
||||||
},
|
},
|
||||||
|
sym::mem_drop if is_copy && !drop_is_single_call_in_arm => {
|
||||||
|
cx.emit_spanned_lint(DROP_COPY, expr.span, DropCopyDiag { arg_ty, note: arg.span });
|
||||||
|
}
|
||||||
_ => return,
|
_ => return,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -671,6 +671,14 @@ pub struct DropRefDiag<'a> {
|
||||||
pub note: Span,
|
pub note: Span,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(LintDiagnostic)]
|
||||||
|
#[diag(lint_drop_copy)]
|
||||||
|
pub struct DropCopyDiag<'a> {
|
||||||
|
pub arg_ty: Ty<'a>,
|
||||||
|
#[note]
|
||||||
|
pub note: Span,
|
||||||
|
}
|
||||||
|
|
||||||
// hidden_unicode_codepoints.rs
|
// hidden_unicode_codepoints.rs
|
||||||
#[derive(LintDiagnostic)]
|
#[derive(LintDiagnostic)]
|
||||||
#[diag(lint_hidden_unicode_codepoints)]
|
#[diag(lint_hidden_unicode_codepoints)]
|
||||||
|
|
79
tests/ui/lint/drop_copy.rs
Normal file
79
tests/ui/lint/drop_copy.rs
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
// check-pass
|
||||||
|
|
||||||
|
#![warn(drop_copy)]
|
||||||
|
|
||||||
|
use std::mem::drop;
|
||||||
|
use std::vec::Vec;
|
||||||
|
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
|
struct SomeStruct;
|
||||||
|
|
||||||
|
struct AnotherStruct {
|
||||||
|
x: u8,
|
||||||
|
y: u8,
|
||||||
|
z: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Clone for AnotherStruct {
|
||||||
|
fn clone(&self) -> AnotherStruct {
|
||||||
|
AnotherStruct {
|
||||||
|
x: self.x,
|
||||||
|
y: self.y,
|
||||||
|
z: self.z.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let s1 = SomeStruct {};
|
||||||
|
let s2 = s1;
|
||||||
|
let s3 = &s1;
|
||||||
|
let mut s4 = s1;
|
||||||
|
let ref s5 = s1;
|
||||||
|
|
||||||
|
drop(s1); //~ WARN calls to `std::mem::drop`
|
||||||
|
drop(s2); //~ WARN calls to `std::mem::drop`
|
||||||
|
drop(s3); //~ WARN calls to `std::mem::drop`
|
||||||
|
drop(s4); //~ WARN calls to `std::mem::drop`
|
||||||
|
drop(s5); //~ WARN calls to `std::mem::drop`
|
||||||
|
|
||||||
|
let a1 = AnotherStruct {
|
||||||
|
x: 255,
|
||||||
|
y: 0,
|
||||||
|
z: vec![1, 2, 3],
|
||||||
|
};
|
||||||
|
let a2 = &a1;
|
||||||
|
let mut a3 = a1.clone();
|
||||||
|
let ref a4 = a1;
|
||||||
|
let a5 = a1.clone();
|
||||||
|
|
||||||
|
drop(a2); //~ WARN calls to `std::mem::drop`
|
||||||
|
drop(a3);
|
||||||
|
drop(a4); //~ WARN calls to `std::mem::drop`
|
||||||
|
drop(a5);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unused)]
|
||||||
|
#[allow(clippy::unit_cmp)]
|
||||||
|
fn issue9482(x: u8) {
|
||||||
|
fn println_and<T>(t: T) -> T {
|
||||||
|
println!("foo");
|
||||||
|
t
|
||||||
|
}
|
||||||
|
|
||||||
|
match x {
|
||||||
|
// Don't lint (copy type), we only care about side-effects
|
||||||
|
0 => drop(println_and(12)),
|
||||||
|
// Don't lint (no copy type), we only care about side-effects
|
||||||
|
1 => drop(println_and(String::new())),
|
||||||
|
2 => {
|
||||||
|
// Lint, even if we only care about the side-effect, it's already in a block
|
||||||
|
drop(println_and(13)); //~ WARN calls to `std::mem::drop`
|
||||||
|
},
|
||||||
|
// Lint, idiomatic use is only in body of `Arm`
|
||||||
|
3 if drop(println_and(14)) == () => (), //~ WARN calls to `std::mem::drop`
|
||||||
|
// Lint, not a fn/method call
|
||||||
|
4 => drop(2),//~ WARN calls to `std::mem::drop`
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
128
tests/ui/lint/drop_copy.stderr
Normal file
128
tests/ui/lint/drop_copy.stderr
Normal file
|
@ -0,0 +1,128 @@
|
||||||
|
warning: calls to `std::mem::drop` with a value that implements `Copy`.
|
||||||
|
--> $DIR/drop_copy.rs:34:5
|
||||||
|
|
|
||||||
|
LL | drop(s1);
|
||||||
|
| ^^^^^^^^
|
||||||
|
|
|
||||||
|
note: argument has type `SomeStruct`
|
||||||
|
--> $DIR/drop_copy.rs:34:10
|
||||||
|
|
|
||||||
|
LL | drop(s1);
|
||||||
|
| ^^
|
||||||
|
note: the lint level is defined here
|
||||||
|
--> $DIR/drop_copy.rs:3:9
|
||||||
|
|
|
||||||
|
LL | #![warn(drop_copy)]
|
||||||
|
| ^^^^^^^^^
|
||||||
|
|
||||||
|
warning: calls to `std::mem::drop` with a value that implements `Copy`.
|
||||||
|
--> $DIR/drop_copy.rs:35:5
|
||||||
|
|
|
||||||
|
LL | drop(s2);
|
||||||
|
| ^^^^^^^^
|
||||||
|
|
|
||||||
|
note: argument has type `SomeStruct`
|
||||||
|
--> $DIR/drop_copy.rs:35:10
|
||||||
|
|
|
||||||
|
LL | drop(s2);
|
||||||
|
| ^^
|
||||||
|
|
||||||
|
warning: calls to `std::mem::drop` with a reference instead of an owned value
|
||||||
|
--> $DIR/drop_copy.rs:36:5
|
||||||
|
|
|
||||||
|
LL | drop(s3);
|
||||||
|
| ^^^^^^^^
|
||||||
|
|
|
||||||
|
note: argument has type `&SomeStruct`
|
||||||
|
--> $DIR/drop_copy.rs:36:10
|
||||||
|
|
|
||||||
|
LL | drop(s3);
|
||||||
|
| ^^
|
||||||
|
= note: `#[warn(drop_ref)]` on by default
|
||||||
|
|
||||||
|
warning: calls to `std::mem::drop` with a value that implements `Copy`.
|
||||||
|
--> $DIR/drop_copy.rs:37:5
|
||||||
|
|
|
||||||
|
LL | drop(s4);
|
||||||
|
| ^^^^^^^^
|
||||||
|
|
|
||||||
|
note: argument has type `SomeStruct`
|
||||||
|
--> $DIR/drop_copy.rs:37:10
|
||||||
|
|
|
||||||
|
LL | drop(s4);
|
||||||
|
| ^^
|
||||||
|
|
||||||
|
warning: calls to `std::mem::drop` with a reference instead of an owned value
|
||||||
|
--> $DIR/drop_copy.rs:38:5
|
||||||
|
|
|
||||||
|
LL | drop(s5);
|
||||||
|
| ^^^^^^^^
|
||||||
|
|
|
||||||
|
note: argument has type `&SomeStruct`
|
||||||
|
--> $DIR/drop_copy.rs:38:10
|
||||||
|
|
|
||||||
|
LL | drop(s5);
|
||||||
|
| ^^
|
||||||
|
|
||||||
|
warning: calls to `std::mem::drop` with a reference instead of an owned value
|
||||||
|
--> $DIR/drop_copy.rs:50:5
|
||||||
|
|
|
||||||
|
LL | drop(a2);
|
||||||
|
| ^^^^^^^^
|
||||||
|
|
|
||||||
|
note: argument has type `&AnotherStruct`
|
||||||
|
--> $DIR/drop_copy.rs:50:10
|
||||||
|
|
|
||||||
|
LL | drop(a2);
|
||||||
|
| ^^
|
||||||
|
|
||||||
|
warning: calls to `std::mem::drop` with a reference instead of an owned value
|
||||||
|
--> $DIR/drop_copy.rs:52:5
|
||||||
|
|
|
||||||
|
LL | drop(a4);
|
||||||
|
| ^^^^^^^^
|
||||||
|
|
|
||||||
|
note: argument has type `&AnotherStruct`
|
||||||
|
--> $DIR/drop_copy.rs:52:10
|
||||||
|
|
|
||||||
|
LL | drop(a4);
|
||||||
|
| ^^
|
||||||
|
|
||||||
|
warning: calls to `std::mem::drop` with a value that implements `Copy`.
|
||||||
|
--> $DIR/drop_copy.rs:71:13
|
||||||
|
|
|
||||||
|
LL | drop(println_and(13));
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
note: argument has type `i32`
|
||||||
|
--> $DIR/drop_copy.rs:71:18
|
||||||
|
|
|
||||||
|
LL | drop(println_and(13));
|
||||||
|
| ^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
warning: calls to `std::mem::drop` with a value that implements `Copy`.
|
||||||
|
--> $DIR/drop_copy.rs:74:14
|
||||||
|
|
|
||||||
|
LL | 3 if drop(println_and(14)) == () => (),
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
note: argument has type `i32`
|
||||||
|
--> $DIR/drop_copy.rs:74:19
|
||||||
|
|
|
||||||
|
LL | 3 if drop(println_and(14)) == () => (),
|
||||||
|
| ^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
warning: calls to `std::mem::drop` with a value that implements `Copy`.
|
||||||
|
--> $DIR/drop_copy.rs:76:14
|
||||||
|
|
|
||||||
|
LL | 4 => drop(2),
|
||||||
|
| ^^^^^^^
|
||||||
|
|
|
||||||
|
note: argument has type `i32`
|
||||||
|
--> $DIR/drop_copy.rs:76:19
|
||||||
|
|
|
||||||
|
LL | 4 => drop(2),
|
||||||
|
| ^
|
||||||
|
|
||||||
|
warning: 10 warnings emitted
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue