@@ -69,6 +69,7 @@ mod path_buf_push_overwrite;
69
69
mod range_zip_with_len;
70
70
mod repeat_once;
71
71
mod search_is_some;
72
+ mod seek_from_current;
72
73
mod seek_to_start_instead_of_rewind;
73
74
mod single_char_add_str;
74
75
mod single_char_insert_string;
@@ -3067,6 +3068,49 @@ declare_clippy_lint! {
3067
3068
"iterating on map using `iter` when `keys` or `values` would do"
3068
3069
}
3069
3070
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
+
3070
3114
declare_clippy_lint ! {
3071
3115
/// ### What it does
3072
3116
///
@@ -3222,6 +3266,7 @@ impl_lint_pass!(Methods => [
3222
3266
VEC_RESIZE_TO_ZERO ,
3223
3267
VERBOSE_FILE_READS ,
3224
3268
ITER_KV_MAP ,
3269
+ SEEK_FROM_CURRENT ,
3225
3270
SEEK_TO_START_INSTEAD_OF_REWIND ,
3226
3271
] ) ;
3227
3272
@@ -3638,6 +3683,9 @@ impl Methods {
3638
3683
vec_resize_to_zero:: check ( cx, expr, count_arg, default_arg, span) ;
3639
3684
} ,
3640
3685
( "seek" , [ arg] ) => {
3686
+ if meets_msrv ( self . msrv , msrvs:: SEEK_FROM_CURRENT ) {
3687
+ seek_from_current:: check ( cx, expr, recv, arg) ;
3688
+ }
3641
3689
if meets_msrv ( self . msrv , msrvs:: SEEK_REWIND ) {
3642
3690
seek_to_start_instead_of_rewind:: check ( cx, expr, recv, arg, span) ;
3643
3691
}
0 commit comments