Skip to content

Commit 391b2a6

Browse files
committed
Auto merge of #10096 - feniljain:fix-seek-rewind, r=xFrednet
fix: not suggest seek_to_start_instead_of_rewind when expr is used changelog: [`seek_to_start_instead_of_rewind`]: No longer lints, if the return of `seek` is used. [#10096](rust-lang/rust-clippy#10096) <!-- changelog_checked --> Fixes #10065
2 parents b62319c + c39849a commit 391b2a6

4 files changed

+18
-2
lines changed

clippy_lints/src/methods/seek_to_start_instead_of_rewind.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::ty::implements_trait;
3-
use clippy_utils::{get_trait_def_id, match_def_path, paths};
3+
use clippy_utils::{get_trait_def_id, is_expr_used_or_unified, match_def_path, paths};
44
use rustc_ast::ast::{LitIntType, LitKind};
55
use rustc_errors::Applicability;
66
use rustc_hir::{Expr, ExprKind};
@@ -19,6 +19,10 @@ pub(super) fn check<'tcx>(
1919
// Get receiver type
2020
let ty = cx.typeck_results().expr_ty(recv).peel_refs();
2121

22+
if is_expr_used_or_unified(cx.tcx, expr) {
23+
return;
24+
}
25+
2226
if let Some(seek_trait_id) = get_trait_def_id(cx, &paths::STD_IO_SEEK) &&
2327
implements_trait(cx, ty, seek_trait_id, &[]) &&
2428
let ExprKind::Call(func, args1) = arg.kind &&

tests/ui/seek_to_start_instead_of_rewind.fixed

+6
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ fn seek_to_end<T: Seek>(t: &mut T) {
7070
t.seek(SeekFrom::End(0));
7171
}
7272

73+
// This should NOT trigger clippy warning because
74+
// expr is used here
75+
fn seek_to_start_in_let<T: Seek>(t: &mut T) {
76+
let a = t.seek(SeekFrom::Start(0)).unwrap();
77+
}
78+
7379
fn main() {
7480
let mut f = OpenOptions::new()
7581
.write(true)

tests/ui/seek_to_start_instead_of_rewind.rs

+6
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ fn seek_to_end<T: Seek>(t: &mut T) {
7070
t.seek(SeekFrom::End(0));
7171
}
7272

73+
// This should NOT trigger clippy warning because
74+
// expr is used here
75+
fn seek_to_start_in_let<T: Seek>(t: &mut T) {
76+
let a = t.seek(SeekFrom::Start(0)).unwrap();
77+
}
78+
7379
fn main() {
7480
let mut f = OpenOptions::new()
7581
.write(true)

tests/ui/seek_to_start_instead_of_rewind.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LL | t.seek(SeekFrom::Start(0));
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `rewind()`
1414

1515
error: used `seek` to go to the start of the stream
16-
--> $DIR/seek_to_start_instead_of_rewind.rs:128:7
16+
--> $DIR/seek_to_start_instead_of_rewind.rs:134:7
1717
|
1818
LL | f.seek(SeekFrom::Start(0));
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `rewind()`

0 commit comments

Comments
 (0)