Skip to content

Commit

Permalink
Auto merge of rust-lang#10096 - feniljain:fix-seek-rewind, r=xFrednet
Browse files Browse the repository at this point in the history
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.
[rust-lang#10096](rust-lang/rust-clippy#10096)
<!-- changelog_checked -->

Fixes rust-lang#10065
  • Loading branch information
bors committed Dec 17, 2022
2 parents b62319c + c39849a commit 391b2a6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
6 changes: 5 additions & 1 deletion clippy_lints/src/methods/seek_to_start_instead_of_rewind.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::ty::implements_trait;
use clippy_utils::{get_trait_def_id, match_def_path, paths};
use clippy_utils::{get_trait_def_id, is_expr_used_or_unified, match_def_path, paths};
use rustc_ast::ast::{LitIntType, LitKind};
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind};
Expand All @@ -19,6 +19,10 @@ pub(super) fn check<'tcx>(
// Get receiver type
let ty = cx.typeck_results().expr_ty(recv).peel_refs();

if is_expr_used_or_unified(cx.tcx, expr) {
return;
}

if let Some(seek_trait_id) = get_trait_def_id(cx, &paths::STD_IO_SEEK) &&
implements_trait(cx, ty, seek_trait_id, &[]) &&
let ExprKind::Call(func, args1) = arg.kind &&
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/seek_to_start_instead_of_rewind.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ fn seek_to_end<T: Seek>(t: &mut T) {
t.seek(SeekFrom::End(0));
}

// This should NOT trigger clippy warning because
// expr is used here
fn seek_to_start_in_let<T: Seek>(t: &mut T) {
let a = t.seek(SeekFrom::Start(0)).unwrap();
}

fn main() {
let mut f = OpenOptions::new()
.write(true)
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/seek_to_start_instead_of_rewind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ fn seek_to_end<T: Seek>(t: &mut T) {
t.seek(SeekFrom::End(0));
}

// This should NOT trigger clippy warning because
// expr is used here
fn seek_to_start_in_let<T: Seek>(t: &mut T) {
let a = t.seek(SeekFrom::Start(0)).unwrap();
}

fn main() {
let mut f = OpenOptions::new()
.write(true)
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/seek_to_start_instead_of_rewind.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ LL | t.seek(SeekFrom::Start(0));
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `rewind()`

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

0 comments on commit 391b2a6

Please sign in to comment.