Skip to content

Commit 1e8d2c5

Browse files
committed
Auto merge of rust-lang#10478 - Jarcho:block_eq, r=xFrednet
`SpanlessEq` improvements fixes rust-lang#9775 Also includes a simplification to `consts::constant`'s interface since I was already touching the code. At the start of `eq_expr` the check: ```rust if !self.inner.allow_side_effects && left.span.ctxt() != right.span.ctxt() { return false; } ``` was removed. This was added in 49e2501 to handle `cfg` macros. This is better handled by the newly added `check_ctxt`. changelog: [various lints]: Don't consider different `cfg!` expansions to be the same unless they are for the same config. changelog: [various lints]: Don't consider the expansion of two different macros to be equal, even when they expand to the same token sequence. changelog: [various lints]: Don't consider two blocks to be equal if they contain disabled code or empty macro expansions, unless those section contain the exact same token sequence.
2 parents 392e955 + 58132cb commit 1e8d2c5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+523
-230
lines changed

clippy_lints/src/assertions_on_constants.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnConstants {
3838
_ => return,
3939
};
4040
let Some((condition, panic_expn)) = find_assert_args(cx, e, macro_call.expn) else { return };
41-
let Some((Constant::Bool(val), _)) = constant(cx, cx.typeck_results(), condition) else { return };
41+
let Some(Constant::Bool(val)) = constant(cx, cx.typeck_results(), condition) else { return };
4242
if val {
4343
span_lint_and_help(
4444
cx,

clippy_lints/src/casts/cast_nan_to_int.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>,
2121

2222
fn is_known_nan(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
2323
match constant(cx, cx.typeck_results(), e) {
24-
Some((Constant::F64(n), _)) => n.is_nan(),
25-
Some((Constant::F32(n), _)) => n.is_nan(),
24+
Some(Constant::F64(n)) => n.is_nan(),
25+
Some(Constant::F32(n)) => n.is_nan(),
2626
_ => false,
2727
}
2828
}

clippy_lints/src/casts/cast_possible_truncation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_target::abi::IntegerType;
1515
use super::{utils, CAST_ENUM_TRUNCATION, CAST_POSSIBLE_TRUNCATION};
1616

1717
fn constant_int(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<u128> {
18-
if let Some((Constant::Int(c), _)) = constant(cx, cx.typeck_results(), expr) {
18+
if let Some(Constant::Int(c)) = constant(cx, cx.typeck_results(), expr) {
1919
Some(c)
2020
} else {
2121
None

clippy_lints/src/casts/cast_sign_loss.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn should_lint(cx: &LateContext<'_>, cast_op: &Expr<'_>, cast_from: Ty<'_>, cast
2929
// Don't lint for positive constants.
3030
let const_val = constant(cx, cx.typeck_results(), cast_op);
3131
if_chain! {
32-
if let Some((Constant::Int(n), _)) = const_val;
32+
if let Some(Constant::Int(n)) = const_val;
3333
if let ty::Int(ity) = *cast_from.kind();
3434
if sext(cx.tcx, n, ity) >= 0;
3535
then {

clippy_lints/src/floating_point_arithmetic.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ declare_lint_pass!(FloatingPointArithmetic => [
114114
// Returns the specialized log method for a given base if base is constant
115115
// and is one of 2, 10 and e
116116
fn get_specialized_log_method(cx: &LateContext<'_>, base: &Expr<'_>) -> Option<&'static str> {
117-
if let Some((value, _)) = constant(cx, cx.typeck_results(), base) {
117+
if let Some(value) = constant(cx, cx.typeck_results(), base) {
118118
if F32(2.0) == value || F64(2.0) == value {
119119
return Some("log2");
120120
} else if F32(10.0) == value || F64(10.0) == value {
@@ -193,8 +193,8 @@ fn check_ln1p(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>) {
193193
constant(cx, cx.typeck_results(), lhs),
194194
constant(cx, cx.typeck_results(), rhs),
195195
) {
196-
(Some((value, _)), _) if F32(1.0) == value || F64(1.0) == value => rhs,
197-
(_, Some((value, _))) if F32(1.0) == value || F64(1.0) == value => lhs,
196+
(Some(value), _) if F32(1.0) == value || F64(1.0) == value => rhs,
197+
(_, Some(value)) if F32(1.0) == value || F64(1.0) == value => lhs,
198198
_ => return,
199199
};
200200

@@ -237,7 +237,7 @@ fn get_integer_from_float_constant(value: &Constant) -> Option<i32> {
237237

238238
fn check_powf(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>, args: &[Expr<'_>]) {
239239
// Check receiver
240-
if let Some((value, _)) = constant(cx, cx.typeck_results(), receiver) {
240+
if let Some(value) = constant(cx, cx.typeck_results(), receiver) {
241241
if let Some(method) = if F32(f32_consts::E) == value || F64(f64_consts::E) == value {
242242
Some("exp")
243243
} else if F32(2.0) == value || F64(2.0) == value {
@@ -258,7 +258,7 @@ fn check_powf(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>, args:
258258
}
259259

260260
// Check argument
261-
if let Some((value, _)) = constant(cx, cx.typeck_results(), &args[0]) {
261+
if let Some(value) = constant(cx, cx.typeck_results(), &args[0]) {
262262
let (lint, help, suggestion) = if F32(1.0 / 2.0) == value || F64(1.0 / 2.0) == value {
263263
(
264264
SUBOPTIMAL_FLOPS,
@@ -298,7 +298,7 @@ fn check_powf(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>, args:
298298
}
299299

300300
fn check_powi(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>, args: &[Expr<'_>]) {
301-
if let Some((value, _)) = constant(cx, cx.typeck_results(), &args[0]) {
301+
if let Some(value) = constant(cx, cx.typeck_results(), &args[0]) {
302302
if value == Int(2) {
303303
if let Some(parent) = get_parent_expr(cx, expr) {
304304
if let Some(grandparent) = get_parent_expr(cx, parent) {
@@ -384,8 +384,8 @@ fn detect_hypot(cx: &LateContext<'_>, receiver: &Expr<'_>) -> Option<String> {
384384
_
385385
) = &add_rhs.kind;
386386
if lmethod_name.as_str() == "powi" && rmethod_name.as_str() == "powi";
387-
if let Some((lvalue, _)) = constant(cx, cx.typeck_results(), largs_1);
388-
if let Some((rvalue, _)) = constant(cx, cx.typeck_results(), rargs_1);
387+
if let Some(lvalue) = constant(cx, cx.typeck_results(), largs_1);
388+
if let Some(rvalue) = constant(cx, cx.typeck_results(), rargs_1);
389389
if Int(2) == lvalue && Int(2) == rvalue;
390390
then {
391391
return Some(format!("{}.hypot({})", Sugg::hir(cx, largs_0, "..").maybe_par(), Sugg::hir(cx, rargs_0, "..")));
@@ -416,7 +416,7 @@ fn check_expm1(cx: &LateContext<'_>, expr: &Expr<'_>) {
416416
if_chain! {
417417
if let ExprKind::Binary(Spanned { node: BinOpKind::Sub, .. }, lhs, rhs) = expr.kind;
418418
if cx.typeck_results().expr_ty(lhs).is_floating_point();
419-
if let Some((value, _)) = constant(cx, cx.typeck_results(), rhs);
419+
if let Some(value) = constant(cx, cx.typeck_results(), rhs);
420420
if F32(1.0) == value || F64(1.0) == value;
421421
if let ExprKind::MethodCall(path, self_arg, ..) = &lhs.kind;
422422
if cx.typeck_results().expr_ty(self_arg).is_floating_point();
@@ -669,8 +669,8 @@ fn check_radians(cx: &LateContext<'_>, expr: &Expr<'_>) {
669669
mul_lhs,
670670
mul_rhs,
671671
) = &div_lhs.kind;
672-
if let Some((rvalue, _)) = constant(cx, cx.typeck_results(), div_rhs);
673-
if let Some((lvalue, _)) = constant(cx, cx.typeck_results(), mul_rhs);
672+
if let Some(rvalue) = constant(cx, cx.typeck_results(), div_rhs);
673+
if let Some(lvalue) = constant(cx, cx.typeck_results(), mul_rhs);
674674
then {
675675
// TODO: also check for constant values near PI/180 or 180/PI
676676
if (F32(f32_consts::PI) == rvalue || F64(f64_consts::PI) == rvalue) &&

clippy_lints/src/fn_null_check.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,7 @@ impl<'tcx> LateLintPass<'tcx> for FnNullCheck {
8989

9090
// Catching:
9191
// (fn_ptr as *<const/mut> <ty>) == <const that evaluates to null_ptr>
92-
_ if matches!(
93-
constant(cx, cx.typeck_results(), to_check),
94-
Some((Constant::RawPtr(0), _))
95-
) =>
96-
{
92+
_ if matches!(constant(cx, cx.typeck_results(), to_check), Some(Constant::RawPtr(0))) => {
9793
lint_expr(cx, expr);
9894
},
9995

clippy_lints/src/implicit_saturating_add.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ fn get_int_max(ty: Ty<'_>) -> Option<u128> {
101101
fn get_const<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) -> Option<(u128, BinOpKind, &'tcx Expr<'tcx>)> {
102102
if let ExprKind::Binary(op, l, r) = expr.kind {
103103
let tr = cx.typeck_results();
104-
if let Some((Constant::Int(c), _)) = constant(cx, tr, r) {
104+
if let Some(Constant::Int(c)) = constant(cx, tr, r) {
105105
return Some((c, op.node, l));
106106
};
107-
if let Some((Constant::Int(c), _)) = constant(cx, tr, l) {
107+
if let Some(Constant::Int(c)) = constant(cx, tr, l) {
108108
return Some((c, invert_op(op.node)?, r));
109109
}
110110
}

clippy_lints/src/index_refutable_slice.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ impl<'a, 'tcx> Visitor<'tcx> for SliceIndexLintingVisitor<'a, 'tcx> {
254254
let parent_id = map.parent_id(expr.hir_id);
255255
if let Some(hir::Node::Expr(parent_expr)) = map.find(parent_id);
256256
if let hir::ExprKind::Index(_, index_expr) = parent_expr.kind;
257-
if let Some((Constant::Int(index_value), _)) = constant(cx, cx.typeck_results(), index_expr);
257+
if let Some(Constant::Int(index_value)) = constant(cx, cx.typeck_results(), index_expr);
258258
if let Ok(index_value) = index_value.try_into();
259259
if index_value < max_suggested_slice;
260260

clippy_lints/src/indexing_slicing.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,18 +191,14 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
191191
/// Returns a tuple of options with the start and end (exclusive) values of
192192
/// the range. If the start or end is not constant, None is returned.
193193
fn to_const_range(cx: &LateContext<'_>, range: higher::Range<'_>, array_size: u128) -> (Option<u128>, Option<u128>) {
194-
let s = range
195-
.start
196-
.map(|expr| constant(cx, cx.typeck_results(), expr).map(|(c, _)| c));
194+
let s = range.start.map(|expr| constant(cx, cx.typeck_results(), expr));
197195
let start = match s {
198196
Some(Some(Constant::Int(x))) => Some(x),
199197
Some(_) => None,
200198
None => Some(0),
201199
};
202200

203-
let e = range
204-
.end
205-
.map(|expr| constant(cx, cx.typeck_results(), expr).map(|(c, _)| c));
201+
let e = range.end.map(|expr| constant(cx, cx.typeck_results(), expr));
206202
let end = match e {
207203
Some(Some(Constant::Int(x))) => {
208204
if range.limits == RangeLimits::Closed {

clippy_lints/src/manual_strip.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ fn len_arg<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<&'tcx E
144144

145145
// Returns the length of the `expr` if it's a constant string or char.
146146
fn constant_length(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<u128> {
147-
let (value, _) = constant(cx, cx.typeck_results(), expr)?;
147+
let value = constant(cx, cx.typeck_results(), expr)?;
148148
match value {
149149
Constant::Str(value) => Some(value.len() as u128),
150150
Constant::Char(value) => Some(value.len_utf8() as u128),

0 commit comments

Comments
 (0)