@@ -69,6 +69,7 @@ mod path_buf_push_overwrite;
6969mod range_zip_with_len;
7070mod repeat_once;
7171mod search_is_some;
72+ mod seek_from_current;
7273mod seek_to_start_instead_of_rewind;
7374mod single_char_add_str;
7475mod single_char_insert_string;
@@ -3067,6 +3068,49 @@ declare_clippy_lint! {
30673068 "iterating on map using `iter` when `keys` or `values` would do"
30683069}
30693070
3071+ declare_clippy_lint ! {
3072+ /// ### What it does
3073+ ///
3074+ /// Checks an argument of `seek` method of `Seek` trait
3075+ /// and if it start seek from `SeekFrom::Current(0)`, suggests `stream_position` instead.
3076+ ///
3077+ /// ### Why is this bad?
3078+ ///
3079+ /// Readability. Use dedicated method.
3080+ ///
3081+ /// ### Example
3082+ ///
3083+ /// ```rust
3084+ /// use std::fs::File;
3085+ /// use std::io::{self, Write, Seek, SeekFrom};
3086+ ///
3087+ /// fn main() -> io::Result<()> {
3088+ /// let mut f = File::create("foo.txt")?;
3089+ /// f.write_all(b"Hello")?;
3090+ /// eprintln!("Written {} bytes", f.seek(SeekFrom::Current(0))?);
3091+ ///
3092+ /// Ok(())
3093+ /// }
3094+ /// ```
3095+ /// Use instead:
3096+ /// ```rust
3097+ /// use std::fs::File;
3098+ /// use std::io::{self, Write, Seek, SeekFrom};
3099+ ///
3100+ /// fn main() -> io::Result<()> {
3101+ /// let mut f = File::create("foo.txt")?;
3102+ /// f.write_all(b"Hello")?;
3103+ /// eprintln!("Written {} bytes", f.stream_position()?);
3104+ ///
3105+ /// Ok(())
3106+ /// }
3107+ /// ```
3108+ #[ clippy:: version = "1.66.0" ]
3109+ pub SEEK_FROM_CURRENT ,
3110+ complexity,
3111+ "use dedicated method for seek from current position"
3112+ }
3113+
30703114declare_clippy_lint ! {
30713115 /// ### What it does
30723116 ///
@@ -3222,6 +3266,7 @@ impl_lint_pass!(Methods => [
32223266 VEC_RESIZE_TO_ZERO ,
32233267 VERBOSE_FILE_READS ,
32243268 ITER_KV_MAP ,
3269+ SEEK_FROM_CURRENT ,
32253270 SEEK_TO_START_INSTEAD_OF_REWIND ,
32263271] ) ;
32273272
@@ -3638,6 +3683,9 @@ impl Methods {
36383683 vec_resize_to_zero:: check ( cx, expr, count_arg, default_arg, span) ;
36393684 } ,
36403685 ( "seek" , [ arg] ) => {
3686+ if meets_msrv ( self . msrv , msrvs:: SEEK_FROM_CURRENT ) {
3687+ seek_from_current:: check ( cx, expr, recv, arg) ;
3688+ }
36413689 if meets_msrv ( self . msrv , msrvs:: SEEK_REWIND ) {
36423690 seek_to_start_instead_of_rewind:: check ( cx, expr, recv, arg, span) ;
36433691 }
0 commit comments