diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index e794af7d64b6..7b0a676e89e3 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -3638,7 +3638,9 @@ fn check_methods<'tcx>(&self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { vec_resize_to_zero::check(cx, expr, count_arg, default_arg, span); }, ("seek", [arg]) => { - rewind_instead_of_seek_to_start::check(cx, expr, recv, arg, span); + if meets_msrv(self.msrv, msrvs::SEEK_REWIND) { + rewind_instead_of_seek_to_start::check(cx, expr, recv, arg, span); + } }, ("sort", []) => { stable_sort_primitive::check(cx, expr, recv); diff --git a/clippy_utils/src/msrvs.rs b/clippy_utils/src/msrvs.rs index 8b843732a236..6f77155ec3e8 100644 --- a/clippy_utils/src/msrvs.rs +++ b/clippy_utils/src/msrvs.rs @@ -37,4 +37,5 @@ macro_rules! msrv_aliases { 1,18,0 { HASH_MAP_RETAIN, HASH_SET_RETAIN } 1,17,0 { FIELD_INIT_SHORTHAND, STATIC_IN_CONST, EXPECT_ERR } 1,16,0 { STR_REPEAT } + 1,55,0 { SEEK_REWIND } } diff --git a/tests/ui/rewind_instead_of_seek_to_start.fixed b/tests/ui/rewind_instead_of_seek_to_start.fixed index 037a288b69b5..36853780977a 100644 --- a/tests/ui/rewind_instead_of_seek_to_start.fixed +++ b/tests/ui/rewind_instead_of_seek_to_start.fixed @@ -1,5 +1,6 @@ // run-rustfix #![allow(unused)] +#![feature(custom_inner_attributes)] #![warn(clippy::rewind_instead_of_seek_to_start)] use std::fs::OpenOptions; @@ -92,3 +93,45 @@ fn main() { assert_eq!(&buf, hello); } + +fn msrv_1_54() { + #![clippy::msrv = "1.54"] + + let mut f = OpenOptions::new() + .write(true) + .read(true) + .create(true) + .open("foo.txt") + .unwrap(); + + let hello = "Hello!\n"; + write!(f, "{hello}").unwrap(); + + f.seek(SeekFrom::Start(0)); + + let mut buf = String::new(); + f.read_to_string(&mut buf).unwrap(); + + assert_eq!(&buf, hello); +} + +fn msrv_1_55() { + #![clippy::msrv = "1.55"] + + let mut f = OpenOptions::new() + .write(true) + .read(true) + .create(true) + .open("foo.txt") + .unwrap(); + + let hello = "Hello!\n"; + write!(f, "{hello}").unwrap(); + + f.rewind(); + + let mut buf = String::new(); + f.read_to_string(&mut buf).unwrap(); + + assert_eq!(&buf, hello); +} diff --git a/tests/ui/rewind_instead_of_seek_to_start.rs b/tests/ui/rewind_instead_of_seek_to_start.rs index 2622425128d6..0d061b58fbab 100644 --- a/tests/ui/rewind_instead_of_seek_to_start.rs +++ b/tests/ui/rewind_instead_of_seek_to_start.rs @@ -1,5 +1,6 @@ // run-rustfix #![allow(unused)] +#![feature(custom_inner_attributes)] #![warn(clippy::rewind_instead_of_seek_to_start)] use std::fs::OpenOptions; @@ -92,3 +93,45 @@ fn main() { assert_eq!(&buf, hello); } + +fn msrv_1_54() { + #![clippy::msrv = "1.54"] + + let mut f = OpenOptions::new() + .write(true) + .read(true) + .create(true) + .open("foo.txt") + .unwrap(); + + let hello = "Hello!\n"; + write!(f, "{hello}").unwrap(); + + f.seek(SeekFrom::Start(0)); + + let mut buf = String::new(); + f.read_to_string(&mut buf).unwrap(); + + assert_eq!(&buf, hello); +} + +fn msrv_1_55() { + #![clippy::msrv = "1.55"] + + let mut f = OpenOptions::new() + .write(true) + .read(true) + .create(true) + .open("foo.txt") + .unwrap(); + + let hello = "Hello!\n"; + write!(f, "{hello}").unwrap(); + + f.seek(SeekFrom::Start(0)); + + let mut buf = String::new(); + f.read_to_string(&mut buf).unwrap(); + + assert_eq!(&buf, hello); +} diff --git a/tests/ui/rewind_instead_of_seek_to_start.stderr b/tests/ui/rewind_instead_of_seek_to_start.stderr index f985471ac057..e80867810842 100644 --- a/tests/ui/rewind_instead_of_seek_to_start.stderr +++ b/tests/ui/rewind_instead_of_seek_to_start.stderr @@ -1,5 +1,5 @@ error: used `seek` to go to the start of the stream - --> $DIR/rewind_instead_of_seek_to_start.rs:53:7 + --> $DIR/rewind_instead_of_seek_to_start.rs:54:7 | LL | t.seek(SeekFrom::Start(0)); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `rewind()` @@ -7,10 +7,16 @@ LL | t.seek(SeekFrom::Start(0)); = note: `-D clippy::rewind-instead-of-seek-to-start` implied by `-D warnings` error: used `seek` to go to the start of the stream - --> $DIR/rewind_instead_of_seek_to_start.rs:58:7 + --> $DIR/rewind_instead_of_seek_to_start.rs:59:7 | LL | t.seek(SeekFrom::Start(0)); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `rewind()` -error: aborting due to 2 previous errors +error: used `seek` to go to the start of the stream + --> $DIR/rewind_instead_of_seek_to_start.rs:131:7 + | +LL | f.seek(SeekFrom::Start(0)); + | ^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `rewind()` + +error: aborting due to 3 previous errors