1 use clippy_utils::diagnostics::span_lint_and_note;
2 use if_chain::if_chain;
3 use rustc_ast::LitKind;
4 use rustc_hir::{Expr, ExprKind};
5 use rustc_lint::LateContext;
6 use rustc_span::source_map::Spanned;
8 use super::SUSPICIOUS_SPLITN;
10 pub(super) fn check(cx: &LateContext<'_>, method_name: &str, expr: &Expr<'_>, self_arg: &Expr<'_>, count: u128) {
13 if let Some(call_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
14 if let Some(impl_id) = cx.tcx.impl_of_method(call_id);
15 if cx.tcx.impl_trait_ref(impl_id).is_none();
16 let self_ty = cx.tcx.type_of(impl_id);
17 if self_ty.is_slice() || self_ty.is_str();
19 // Ignore empty slice and string literals when used with a literal count.
20 if matches!(self_arg.kind, ExprKind::Array([]))
21 || matches!(self_arg.kind, ExprKind::Lit(Spanned { node: LitKind::Str(s, _), .. }) if s.is_empty())
26 let (msg, note_msg) = if count == 0 {
27 (format!("`{}` called with `0` splits", method_name),
28 "the resulting iterator will always return `None`")
30 (format!("`{}` called with `1` split", method_name),
31 if self_ty.is_slice() {
32 "the resulting iterator will always return the entire slice followed by `None`"
34 "the resulting iterator will always return the entire string followed by `None`"