about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbluthej <joffrey.bluthe@e.email>2023-03-18 10:43:02 +0100
committerbluthej <joffrey.bluthe@e.email>2023-03-21 22:51:51 +0100
commit7f44530f54002401ba771bcc19f56f4950b22bb1 (patch)
tree5c2613330a4e2464e272a50ccbb95b60d66b7356
parent6cebe58dfe09654100fe370773e3d10581e20bdf (diff)
downloadrust-7f44530f54002401ba771bcc19f56f4950b22bb1.tar.gz
rust-7f44530f54002401ba771bcc19f56f4950b22bb1.zip
Create `clear_with_drain` lint
-rw-r--r--CHANGELOG.md1
-rw-r--r--clippy_lints/src/declared_lints.rs1
-rw-r--r--clippy_lints/src/methods/clear_with_drain.rs8
-rw-r--r--clippy_lints/src/methods/mod.rs21
-rw-r--r--tests/ui/clear_with_drain.rs12
5 files changed, 43 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 47a503510c1..81d0ff80968 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4441,6 +4441,7 @@ Released 2018-09-13
 [`chars_last_cmp`]: https://rust-lang.github.io/rust-clippy/master/index.html#chars_last_cmp
 [`chars_next_cmp`]: https://rust-lang.github.io/rust-clippy/master/index.html#chars_next_cmp
 [`checked_conversions`]: https://rust-lang.github.io/rust-clippy/master/index.html#checked_conversions
+[`clear_with_drain`]: https://rust-lang.github.io/rust-clippy/master/index.html#clear_with_drain
 [`clone_double_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#clone_double_ref
 [`clone_on_copy`]: https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
 [`clone_on_ref_ptr`]: https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_ref_ptr
diff --git a/clippy_lints/src/declared_lints.rs b/clippy_lints/src/declared_lints.rs
index 2331e857b1f..f7b108c923a 100644
--- a/clippy_lints/src/declared_lints.rs
+++ b/clippy_lints/src/declared_lints.rs
@@ -307,6 +307,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
     crate::methods::CASE_SENSITIVE_FILE_EXTENSION_COMPARISONS_INFO,
     crate::methods::CHARS_LAST_CMP_INFO,
     crate::methods::CHARS_NEXT_CMP_INFO,
+    crate::methods::CLEAR_WITH_DRAIN_INFO,
     crate::methods::CLONED_INSTEAD_OF_COPIED_INFO,
     crate::methods::CLONE_DOUBLE_REF_INFO,
     crate::methods::CLONE_ON_COPY_INFO,
diff --git a/clippy_lints/src/methods/clear_with_drain.rs b/clippy_lints/src/methods/clear_with_drain.rs
new file mode 100644
index 00000000000..9e55852ef7a
--- /dev/null
+++ b/clippy_lints/src/methods/clear_with_drain.rs
@@ -0,0 +1,8 @@
+use rustc_lint::{LateContext, LintContext};
+
+use super::CLEAR_WITH_DRAIN;
+
+// TODO: Adjust the parameters as necessary
+pub(super) fn check(cx: &LateContext) {
+    todo!();
+}
diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs
index 56e3988bf09..859284b6a5c 100644
--- a/clippy_lints/src/methods/mod.rs
+++ b/clippy_lints/src/methods/mod.rs
@@ -9,6 +9,7 @@ mod chars_last_cmp;
 mod chars_last_cmp_with_unwrap;
 mod chars_next_cmp;
 mod chars_next_cmp_with_unwrap;
+mod clear_with_drain;
 mod clone_on_copy;
 mod clone_on_ref_ptr;
 mod cloned_instead_of_copied;
@@ -3190,6 +3191,25 @@ declare_clippy_lint! {
     "single command line argument that looks like it should be multiple arguments"
 }
 
+declare_clippy_lint! {
+    /// ### What it does
+    ///
+    /// ### Why is this bad?
+    ///
+    /// ### Example
+    /// ```rust
+    /// // example code where clippy issues a warning
+    /// ```
+    /// Use instead:
+    /// ```rust
+    /// // example code which does not raise clippy warning
+    /// ```
+    #[clippy::version = "1.69.0"]
+    pub CLEAR_WITH_DRAIN,
+    nursery,
+    "default lint description"
+}
+
 pub struct Methods {
     avoid_breaking_exported_api: bool,
     msrv: Msrv,
@@ -3318,6 +3338,7 @@ impl_lint_pass!(Methods => [
     SEEK_TO_START_INSTEAD_OF_REWIND,
     NEEDLESS_COLLECT,
     SUSPICIOUS_COMMAND_ARG_SPACE,
+    CLEAR_WITH_DRAIN,
 ]);
 
 /// Extracts a method call name, args, and `Span` of the method name.
diff --git a/tests/ui/clear_with_drain.rs b/tests/ui/clear_with_drain.rs
new file mode 100644
index 00000000000..bb3a371cc08
--- /dev/null
+++ b/tests/ui/clear_with_drain.rs
@@ -0,0 +1,12 @@
+#![allow(unused)]
+#![warn(clippy::clear_with_drain)]
+
+fn main() {
+    let mut vec: Vec<i32> = Vec::new();
+    //Lint
+    vec.drain(..);
+    vec.drain(0..vec.len());
+
+    // Dont Lint
+    let iter = vec.drain(..);
+}