};
declare_clippy_lint! {
- /// **What it does:** Checks for getting the length of something via `.len()`
+ /// ### What it does
+ /// Checks for getting the length of something via `.len()`
/// just to compare to zero, and suggests using `.is_empty()` where applicable.
///
- /// **Why is this bad?** Some structures can answer `.is_empty()` much faster
+ /// ### Why is this bad?
+ /// Some structures can answer `.is_empty()` much faster
/// than calculating their length. So it is good to get into the habit of using
/// `.is_empty()`, and having it is cheap.
/// Besides, it makes the intent clearer than a manual comparison in some contexts.
///
- /// **Known problems:** None.
- ///
- /// **Example:**
+ /// ### Example
/// ```ignore
/// if x.len() == 0 {
/// ..
}
declare_clippy_lint! {
- /// **What it does:** Checks for items that implement `.len()` but not
+ /// ### What it does
+ /// Checks for items that implement `.len()` but not
/// `.is_empty()`.
///
- /// **Why is this bad?** It is good custom to have both methods, because for
+ /// ### Why is this bad?
+ /// It is good custom to have both methods, because for
/// some data structures, asking about the length will be a costly operation,
/// whereas `.is_empty()` can usually answer in constant time. Also it used to
/// lead to false positives on the [`len_zero`](#len_zero) lint – currently that
/// lint will ignore such entities.
///
- /// **Known problems:** None.
- ///
- /// **Example:**
+ /// ### Example
/// ```ignore
/// impl X {
/// pub fn len(&self) -> usize {
}
declare_clippy_lint! {
- /// **What it does:** Checks for comparing to an empty slice such as `""` or `[]`,
+ /// ### What it does
+ /// Checks for comparing to an empty slice such as `""` or `[]`,
/// and suggests using `.is_empty()` where applicable.
///
- /// **Why is this bad?** Some structures can answer `.is_empty()` much faster
+ /// ### Why is this bad?
+ /// Some structures can answer `.is_empty()` much faster
/// than checking for equality. So it is good to get into the habit of using
/// `.is_empty()`, and having it is cheap.
/// Besides, it makes the intent clearer than a manual comparison in some contexts.
///
- /// **Known problems:** None.
- ///
- /// **Example:**
+ /// ### Example
///
/// ```ignore
/// if s == "" {
if item.ident.name == sym::len;
if let ImplItemKind::Fn(sig, _) = &item.kind;
if sig.decl.implicit_self.has_implicit_self();
- if cx.access_levels.is_exported(item.hir_id());
+ if cx.access_levels.is_exported(item.def_id);
if matches!(sig.decl.output, FnRetTy::Return(_));
if let Some(imp) = get_parent_as_impl(cx.tcx, item.hir_id());
if imp.of_trait.is_none();
}
}
- if cx.access_levels.is_exported(visited_trait.hir_id())
- && trait_items.iter().any(|i| is_named_self(cx, i, sym::len))
+ if cx.access_levels.is_exported(visited_trait.def_id) && trait_items.iter().any(|i| is_named_self(cx, i, sym::len))
{
let mut current_and_super_traits = DefIdSet::default();
fill_trait_set(visited_trait.def_id.to_def_id(), &mut current_and_super_traits, cx);
None,
None,
),
- Some(is_empty)
- if !cx
- .access_levels
- .is_exported(cx.tcx.hir().local_def_id_to_hir_id(is_empty.def_id.expect_local())) =>
- {
- (
- format!(
- "{} `{}` has a public `len` method, but a private `is_empty` method",
- item_kind,
- item_name.as_str(),
- ),
- Some(cx.tcx.def_span(is_empty.def_id)),
- None,
- )
- },
+ Some(is_empty) if !cx.access_levels.is_exported(is_empty.def_id.expect_local()) => (
+ format!(
+ "{} `{}` has a public `len` method, but a private `is_empty` method",
+ item_kind,
+ item_name.as_str(),
+ ),
+ Some(cx.tcx.def_span(is_empty.def_id)),
+ None,
+ ),
Some(is_empty)
if !(is_empty.fn_has_self_parameter
&& check_is_empty_sig(cx.tcx.fn_sig(is_empty.def_id).skip_binder(), self_kind, output)) =>