mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-27 15:11:30 +00:00
Pull is_full_range
method from iter_with_drain
Rename method to `is_range_full` because the type is actually `RangeFull`. Method moved to `clippy_utils` for reuse in `clear_with_drain`.
This commit is contained in:
parent
7f44530f54
commit
85d428101d
2 changed files with 23 additions and 21 deletions
|
@ -1,9 +1,8 @@
|
||||||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||||
use clippy_utils::higher::Range;
|
use clippy_utils::higher::Range;
|
||||||
use clippy_utils::is_integer_const;
|
use clippy_utils::is_range_full;
|
||||||
use rustc_ast::ast::RangeLimits;
|
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::{Expr, ExprKind, QPath};
|
use rustc_hir::{Expr, ExprKind};
|
||||||
use rustc_lint::LateContext;
|
use rustc_lint::LateContext;
|
||||||
use rustc_span::symbol::sym;
|
use rustc_span::symbol::sym;
|
||||||
use rustc_span::Span;
|
use rustc_span::Span;
|
||||||
|
@ -16,7 +15,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, span
|
||||||
&& let Some(ty_name) = cx.tcx.get_diagnostic_name(adt.did())
|
&& let Some(ty_name) = cx.tcx.get_diagnostic_name(adt.did())
|
||||||
&& matches!(ty_name, sym::Vec | sym::VecDeque)
|
&& matches!(ty_name, sym::Vec | sym::VecDeque)
|
||||||
&& let Some(range) = Range::hir(arg)
|
&& let Some(range) = Range::hir(arg)
|
||||||
&& is_full_range(cx, recv, range)
|
&& is_range_full(cx, recv, range)
|
||||||
{
|
{
|
||||||
span_lint_and_sugg(
|
span_lint_and_sugg(
|
||||||
cx,
|
cx,
|
||||||
|
@ -29,19 +28,3 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, span
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_full_range(cx: &LateContext<'_>, container: &Expr<'_>, range: Range<'_>) -> bool {
|
|
||||||
range.start.map_or(true, |e| is_integer_const(cx, e, 0))
|
|
||||||
&& range.end.map_or(true, |e| {
|
|
||||||
if range.limits == RangeLimits::HalfOpen
|
|
||||||
&& let ExprKind::Path(QPath::Resolved(None, container_path)) = container.kind
|
|
||||||
&& let ExprKind::MethodCall(name, self_arg, [], _) = e.kind
|
|
||||||
&& name.ident.name == sym::len
|
|
||||||
&& let ExprKind::Path(QPath::Resolved(None, path)) = self_arg.kind
|
|
||||||
{
|
|
||||||
container_path.res == path.res
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
|
@ -78,7 +78,7 @@ use std::sync::OnceLock;
|
||||||
use std::sync::{Mutex, MutexGuard};
|
use std::sync::{Mutex, MutexGuard};
|
||||||
|
|
||||||
use if_chain::if_chain;
|
use if_chain::if_chain;
|
||||||
use rustc_ast::ast::{self, LitKind};
|
use rustc_ast::ast::{self, LitKind, RangeLimits};
|
||||||
use rustc_ast::Attribute;
|
use rustc_ast::Attribute;
|
||||||
use rustc_data_structures::fx::FxHashMap;
|
use rustc_data_structures::fx::FxHashMap;
|
||||||
use rustc_data_structures::unhash::UnhashMap;
|
use rustc_data_structures::unhash::UnhashMap;
|
||||||
|
@ -115,6 +115,7 @@ use rustc_span::Span;
|
||||||
use rustc_target::abi::Integer;
|
use rustc_target::abi::Integer;
|
||||||
|
|
||||||
use crate::consts::{constant, Constant};
|
use crate::consts::{constant, Constant};
|
||||||
|
use crate::higher::Range;
|
||||||
use crate::ty::{can_partially_move_ty, expr_sig, is_copy, is_recursively_primitive_type, ty_is_fn_once_param};
|
use crate::ty::{can_partially_move_ty, expr_sig, is_copy, is_recursively_primitive_type, ty_is_fn_once_param};
|
||||||
use crate::visitors::for_each_expr;
|
use crate::visitors::for_each_expr;
|
||||||
|
|
||||||
|
@ -1491,6 +1492,24 @@ pub fn is_else_clause(tcx: TyCtxt<'_>, expr: &Expr<'_>) -> bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Checks whether the given `Range` is equivalent to a `RangeFull`.
|
||||||
|
/// Inclusive ranges are not considered because they already constitute a lint.
|
||||||
|
pub fn is_range_full(cx: &LateContext<'_>, container: &Expr<'_>, range: Range<'_>) -> bool {
|
||||||
|
range.start.map_or(true, |e| is_integer_const(cx, e, 0))
|
||||||
|
&& range.end.map_or(true, |e| {
|
||||||
|
if range.limits == RangeLimits::HalfOpen
|
||||||
|
&& let ExprKind::Path(QPath::Resolved(None, container_path)) = container.kind
|
||||||
|
&& let ExprKind::MethodCall(name, self_arg, [], _) = e.kind
|
||||||
|
&& name.ident.name == sym::len
|
||||||
|
&& let ExprKind::Path(QPath::Resolved(None, path)) = self_arg.kind
|
||||||
|
{
|
||||||
|
container_path.res == path.res
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/// Checks whether the given expression is a constant integer of the given value.
|
/// Checks whether the given expression is a constant integer of the given value.
|
||||||
/// unlike `is_integer_literal`, this version does const folding
|
/// unlike `is_integer_literal`, this version does const folding
|
||||||
pub fn is_integer_const(cx: &LateContext<'_>, e: &Expr<'_>, value: u128) -> bool {
|
pub fn is_integer_const(cx: &LateContext<'_>, e: &Expr<'_>, value: u128) -> bool {
|
||||||
|
|
Loading…
Reference in a new issue