-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
868dba9
commit 2666c38
Showing
10 changed files
with
386 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
use clippy_utils::diagnostics::span_lint_and_help; | ||
use clippy_utils::ty::{match_type, peel_mid_ty_refs_is_mutable}; | ||
use clippy_utils::{fn_def_id, path_to_local_id, paths, peel_ref_operators}; | ||
use rustc_ast::Mutability; | ||
use rustc_hir::intravisit::{walk_expr, Visitor}; | ||
use rustc_hir::lang_items::LangItem; | ||
use rustc_hir::{Block, Expr, ExprKind, HirId, Local, Node, PatKind, PathSegment, StmtKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_middle::ty::Ty; | ||
use rustc_session::{declare_tool_lint, impl_lint_pass}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for the creation of a `peekable` iterator that is never `.peek()`ed | ||
/// | ||
/// ### Why is this bad? | ||
/// Creating a peekable iterator without using any of its methods is likely a mistake, | ||
/// or just a leftover after a refactor. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// let collection = vec![1, 2, 3]; | ||
/// let iter = collection.iter().peekable(); | ||
/// | ||
/// for item in iter { | ||
/// // ... | ||
/// } | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```rust | ||
/// let collection = vec![1, 2, 3]; | ||
/// let iter = collection.iter(); | ||
/// | ||
/// for item in iter { | ||
/// // ... | ||
/// } | ||
/// ``` | ||
#[clippy::version = "1.64.0"] | ||
pub UNUSED_PEEKABLE, | ||
suspicious, | ||
"creating a peekable iterator without using any of its methods" | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct UnusedPeekable { | ||
visited: Vec<HirId>, | ||
} | ||
|
||
impl_lint_pass!(UnusedPeekable => [UNUSED_PEEKABLE]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for UnusedPeekable { | ||
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &Block<'tcx>) { | ||
// Don't lint `Peekable`s returned from a block | ||
if let Some(expr) = block.expr | ||
&& let Some(ty) = cx.typeck_results().expr_ty_opt(peel_ref_operators(cx, expr)) | ||
&& match_type(cx, ty, &paths::PEEKABLE) | ||
{ | ||
return; | ||
} | ||
|
||
for (idx, stmt) in block.stmts.iter().enumerate() { | ||
if !stmt.span.from_expansion() | ||
&& let StmtKind::Local(local) = stmt.kind | ||
&& !self.visited.contains(&local.pat.hir_id) | ||
&& let PatKind::Binding(_, _, ident, _) = local.pat.kind | ||
&& let Some(init) = local.init | ||
&& !init.span.from_expansion() | ||
&& let Some(ty) = cx.typeck_results().expr_ty_opt(init) | ||
&& let (ty, _, Mutability::Mut) = peel_mid_ty_refs_is_mutable(ty) | ||
&& match_type(cx, ty, &paths::PEEKABLE) | ||
{ | ||
let mut vis = PeekableVisitor::new(cx, local.pat.hir_id); | ||
|
||
if idx + 1 == block.stmts.len() && block.expr.is_none() { | ||
return; | ||
} | ||
|
||
for stmt in &block.stmts[idx..] { | ||
vis.visit_stmt(stmt); | ||
} | ||
|
||
if let Some(expr) = block.expr { | ||
vis.visit_expr(expr); | ||
} | ||
|
||
if !vis.found_peek_call { | ||
span_lint_and_help( | ||
cx, | ||
UNUSED_PEEKABLE, | ||
ident.span, | ||
"`peek` never called on `Peekable` iterator", | ||
None, | ||
"consider removing the call to `peekable`" | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
struct PeekableVisitor<'a, 'tcx> { | ||
cx: &'a LateContext<'tcx>, | ||
expected_hir_id: HirId, | ||
found_peek_call: bool, | ||
} | ||
|
||
impl<'a, 'tcx> PeekableVisitor<'a, 'tcx> { | ||
fn new(cx: &'a LateContext<'tcx>, expected_hir_id: HirId) -> Self { | ||
Self { | ||
cx, | ||
expected_hir_id, | ||
found_peek_call: false, | ||
} | ||
} | ||
} | ||
|
||
impl<'tcx> Visitor<'_> for PeekableVisitor<'_, 'tcx> { | ||
fn visit_expr(&mut self, ex: &'_ Expr<'_>) { | ||
if path_to_local_id(ex, self.expected_hir_id) { | ||
for (_, node) in self.cx.tcx.hir().parent_iter(ex.hir_id) { | ||
match node { | ||
Node::Expr(expr) => { | ||
match expr.kind { | ||
// some_function(peekable) | ||
// | ||
// If the Peekable is passed to a function, stop | ||
ExprKind::Call(_, args) => { | ||
if let Some(func_did) = fn_def_id(self.cx, expr) | ||
&& let Ok(into_iter_did) = self | ||
.cx | ||
.tcx | ||
.lang_items() | ||
.require(LangItem::IntoIterIntoIter) | ||
&& func_did == into_iter_did | ||
{ | ||
// Probably a for loop desugar, stop searching | ||
return; | ||
} | ||
|
||
for arg in args.iter().map(|arg| peel_ref_operators(self.cx, arg)) { | ||
if let ExprKind::Path(_) = arg.kind | ||
&& let Some(ty) = self | ||
.cx | ||
.typeck_results() | ||
.expr_ty_opt(arg) | ||
.map(Ty::peel_refs) | ||
&& match_type(self.cx, ty, &paths::PEEKABLE) | ||
{ | ||
self.found_peek_call = true; | ||
return; | ||
} | ||
} | ||
}, | ||
// Peekable::peek() | ||
ExprKind::MethodCall(PathSegment { ident: method_name, .. }, [arg, ..], _) => { | ||
let arg = peel_ref_operators(self.cx, arg); | ||
let method_name = method_name.name.as_str(); | ||
|
||
if (method_name == "peek" | ||
|| method_name == "peek_mut" | ||
|| method_name == "next_if" | ||
|| method_name == "next_if_eq") | ||
&& let ExprKind::Path(_) = arg.kind | ||
&& let Some(ty) = self.cx.typeck_results().expr_ty_opt(arg).map(Ty::peel_refs) | ||
&& match_type(self.cx, ty, &paths::PEEKABLE) | ||
{ | ||
self.found_peek_call = true; | ||
return; | ||
} | ||
}, | ||
// Don't bother if moved into a struct | ||
ExprKind::Struct(..) => { | ||
self.found_peek_call = true; | ||
return; | ||
}, | ||
_ => {}, | ||
} | ||
}, | ||
Node::Local(Local { init: Some(init), .. }) => { | ||
if let Some(ty) = self.cx.typeck_results().expr_ty_opt(init) | ||
&& let (ty, _, Mutability::Mut) = peel_mid_ty_refs_is_mutable(ty) | ||
&& match_type(self.cx, ty, &paths::PEEKABLE) | ||
{ | ||
self.found_peek_call = true; | ||
return; | ||
} | ||
|
||
break; | ||
}, | ||
Node::Stmt(stmt) => match stmt.kind { | ||
StmtKind::Expr(_) | StmtKind::Semi(_) => {}, | ||
_ => { | ||
self.found_peek_call = true; | ||
return; | ||
}, | ||
}, | ||
Node::Block(_) => {}, | ||
_ => { | ||
break; | ||
}, | ||
} | ||
} | ||
} | ||
|
||
walk_expr(self, ex); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
#![warn(clippy::unused_peekable)] | ||
#![allow(clippy::no_effect)] | ||
|
||
use std::iter::Empty; | ||
use std::iter::Peekable; | ||
|
||
fn main() { | ||
invalid(); | ||
valid(); | ||
} | ||
|
||
#[allow(clippy::unused_unit)] | ||
fn invalid() { | ||
let peekable = std::iter::empty::<u32>().peekable(); | ||
|
||
// Only lint `new_local` | ||
let old_local = std::iter::empty::<u32>().peekable(); | ||
let new_local = old_local; | ||
|
||
// Behind mut ref | ||
let mut by_mut_ref_test = std::iter::empty::<u32>().peekable(); | ||
let by_mut_ref = &mut by_mut_ref_test; | ||
|
||
// Explicitly returns `Peekable` | ||
fn returns_peekable() -> Peekable<Empty<u32>> { | ||
std::iter::empty().peekable() | ||
} | ||
|
||
let peekable_from_fn = returns_peekable(); | ||
|
||
// Using a method not exclusive to `Peekable` | ||
let mut peekable_using_iterator_method = std::iter::empty::<u32>().peekable(); | ||
peekable_using_iterator_method.next(); | ||
|
||
let mut peekable_in_for_loop = std::iter::empty::<u32>().peekable(); | ||
for x in peekable_in_for_loop {} | ||
} | ||
|
||
fn valid() { | ||
fn takes_peekable(_peek: Peekable<Empty<u32>>) {} | ||
|
||
// Passed to another function | ||
let passed_along = std::iter::empty::<u32>().peekable(); | ||
takes_peekable(passed_along); | ||
|
||
// `peek` called in another block | ||
let mut peekable_in_block = std::iter::empty::<u32>().peekable(); | ||
{ | ||
peekable_in_block.peek(); | ||
} | ||
|
||
// Check the other `Peekable` methods :) | ||
{ | ||
let mut peekable_with_peek_mut = std::iter::empty::<u32>().peekable(); | ||
peekable_with_peek_mut.peek_mut(); | ||
|
||
let mut peekable_with_next_if = std::iter::empty::<u32>().peekable(); | ||
peekable_with_next_if.next_if(|_| true); | ||
|
||
let mut peekable_with_next_if_eq = std::iter::empty::<u32>().peekable(); | ||
peekable_with_next_if_eq.next_if_eq(&3); | ||
} | ||
|
||
let mut peekable_in_closure = std::iter::empty::<u32>().peekable(); | ||
let call_peek = |p: &mut Peekable<Empty<u32>>| { | ||
p.peek(); | ||
}; | ||
call_peek(&mut peekable_in_closure); | ||
|
||
// From a macro | ||
macro_rules! make_me_a_peekable_please { | ||
() => { | ||
std::iter::empty::<u32>().peekable() | ||
}; | ||
} | ||
|
||
let _unsuspecting_macro_user = make_me_a_peekable_please!(); | ||
|
||
// Generic Iterator returned | ||
fn return_an_iter() -> impl Iterator<Item = u32> { | ||
std::iter::empty::<u32>().peekable() | ||
} | ||
|
||
let _unsuspecting_user = return_an_iter(); | ||
|
||
// Call `peek` in a macro | ||
macro_rules! peek_iter { | ||
($iter:ident) => { | ||
$iter.peek(); | ||
}; | ||
} | ||
|
||
let mut peek_in_macro = std::iter::empty::<u32>().peekable(); | ||
peek_iter!(peek_in_macro); | ||
|
||
// Behind mut ref | ||
let mut by_mut_ref_test = std::iter::empty::<u32>().peekable(); | ||
let by_mut_ref = &mut by_mut_ref_test; | ||
by_mut_ref.peek(); | ||
|
||
// Behind ref | ||
let mut by_ref_test = std::iter::empty::<u32>().peekable(); | ||
let by_ref = &by_ref_test; | ||
by_ref_test.peek(); | ||
|
||
// In struct | ||
struct PeekableWrapper { | ||
f: Peekable<Empty<u32>>, | ||
} | ||
|
||
let struct_test = std::iter::empty::<u32>().peekable(); | ||
PeekableWrapper { f: struct_test }; | ||
|
||
// `peek` called in another block as the last expression | ||
let mut peekable_last_expr = std::iter::empty::<u32>().peekable(); | ||
{ | ||
peekable_last_expr.peek(); | ||
} | ||
} |
Oops, something went wrong.