Skip to content

Commit 85d4281

Browse files
committed
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`.
1 parent 7f44530 commit 85d4281

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed
Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::higher::Range;
3-
use clippy_utils::is_integer_const;
4-
use rustc_ast::ast::RangeLimits;
3+
use clippy_utils::is_range_full;
54
use rustc_errors::Applicability;
6-
use rustc_hir::{Expr, ExprKind, QPath};
5+
use rustc_hir::{Expr, ExprKind};
76
use rustc_lint::LateContext;
87
use rustc_span::symbol::sym;
98
use rustc_span::Span;
@@ -16,7 +15,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, span
1615
&& let Some(ty_name) = cx.tcx.get_diagnostic_name(adt.did())
1716
&& matches!(ty_name, sym::Vec | sym::VecDeque)
1817
&& let Some(range) = Range::hir(arg)
19-
&& is_full_range(cx, recv, range)
18+
&& is_range_full(cx, recv, range)
2019
{
2120
span_lint_and_sugg(
2221
cx,
@@ -29,19 +28,3 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, span
2928
);
3029
};
3130
}
32-
33-
fn is_full_range(cx: &LateContext<'_>, container: &Expr<'_>, range: Range<'_>) -> bool {
34-
range.start.map_or(true, |e| is_integer_const(cx, e, 0))
35-
&& range.end.map_or(true, |e| {
36-
if range.limits == RangeLimits::HalfOpen
37-
&& let ExprKind::Path(QPath::Resolved(None, container_path)) = container.kind
38-
&& let ExprKind::MethodCall(name, self_arg, [], _) = e.kind
39-
&& name.ident.name == sym::len
40-
&& let ExprKind::Path(QPath::Resolved(None, path)) = self_arg.kind
41-
{
42-
container_path.res == path.res
43-
} else {
44-
false
45-
}
46-
})
47-
}

clippy_utils/src/lib.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ use std::sync::OnceLock;
7878
use std::sync::{Mutex, MutexGuard};
7979

8080
use if_chain::if_chain;
81-
use rustc_ast::ast::{self, LitKind};
81+
use rustc_ast::ast::{self, LitKind, RangeLimits};
8282
use rustc_ast::Attribute;
8383
use rustc_data_structures::fx::FxHashMap;
8484
use rustc_data_structures::unhash::UnhashMap;
@@ -115,6 +115,7 @@ use rustc_span::Span;
115115
use rustc_target::abi::Integer;
116116

117117
use crate::consts::{constant, Constant};
118+
use crate::higher::Range;
118119
use crate::ty::{can_partially_move_ty, expr_sig, is_copy, is_recursively_primitive_type, ty_is_fn_once_param};
119120
use crate::visitors::for_each_expr;
120121

@@ -1491,6 +1492,24 @@ pub fn is_else_clause(tcx: TyCtxt<'_>, expr: &Expr<'_>) -> bool {
14911492
}
14921493
}
14931494

1495+
/// Checks whether the given `Range` is equivalent to a `RangeFull`.
1496+
/// Inclusive ranges are not considered because they already constitute a lint.
1497+
pub fn is_range_full(cx: &LateContext<'_>, container: &Expr<'_>, range: Range<'_>) -> bool {
1498+
range.start.map_or(true, |e| is_integer_const(cx, e, 0))
1499+
&& range.end.map_or(true, |e| {
1500+
if range.limits == RangeLimits::HalfOpen
1501+
&& let ExprKind::Path(QPath::Resolved(None, container_path)) = container.kind
1502+
&& let ExprKind::MethodCall(name, self_arg, [], _) = e.kind
1503+
&& name.ident.name == sym::len
1504+
&& let ExprKind::Path(QPath::Resolved(None, path)) = self_arg.kind
1505+
{
1506+
container_path.res == path.res
1507+
} else {
1508+
false
1509+
}
1510+
})
1511+
}
1512+
14941513
/// Checks whether the given expression is a constant integer of the given value.
14951514
/// unlike `is_integer_literal`, this version does const folding
14961515
pub fn is_integer_const(cx: &LateContext<'_>, e: &Expr<'_>, value: u128) -> bool {

0 commit comments

Comments
 (0)