Skip to content

Commit ea4db3a

Browse files
committed
Auto merge of #8350 - dswij:8331, r=Manishearth
fix bad suggestion on `numeric_literal` closes #8331 changelog: [`numeric_literal`] fix suggestion not showing sign
2 parents a26c412 + 0d7273f commit ea4db3a

File tree

4 files changed

+52
-13
lines changed

4 files changed

+52
-13
lines changed

clippy_lints/src/casts/unnecessary_cast.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,14 @@ pub(super) fn check(
2323

2424
if_chain! {
2525
if let LitKind::Int(n, _) = lit.node;
26-
if let Some(src) = snippet_opt(cx, lit.span);
26+
if let Some(src) = snippet_opt(cx, cast_expr.span);
2727
if cast_to.is_floating_point();
2828
if let Some(num_lit) = NumericLiteral::from_lit_kind(&src, &lit.node);
2929
let from_nbits = 128 - n.leading_zeros();
3030
let to_nbits = fp_ty_mantissa_nbits(cast_to);
3131
if from_nbits != 0 && to_nbits != 0 && from_nbits <= to_nbits && num_lit.is_decimal();
3232
then {
33-
let literal_str = if is_unary_neg(cast_expr) { format!("-{}", num_lit.integer) } else { num_lit.integer.into() };
34-
lint_unnecessary_cast(cx, expr, &literal_str, cast_from, cast_to);
33+
lint_unnecessary_cast(cx, expr, num_lit.integer, cast_from, cast_to);
3534
return true
3635
}
3736
}
@@ -48,7 +47,7 @@ pub(super) fn check(
4847
| LitKind::Float(_, LitFloatType::Suffixed(_))
4948
if cast_from.kind() == cast_to.kind() =>
5049
{
51-
if let Some(src) = snippet_opt(cx, lit.span) {
50+
if let Some(src) = snippet_opt(cx, cast_expr.span) {
5251
if let Some(num_lit) = NumericLiteral::from_lit_kind(&src, &lit.node) {
5352
lint_unnecessary_cast(cx, expr, num_lit.integer, cast_from, cast_to);
5453
}
@@ -113,7 +112,3 @@ fn fp_ty_mantissa_nbits(typ: Ty<'_>) -> u32 {
113112
_ => 0,
114113
}
115114
}
116-
117-
fn is_unary_neg(expr: &Expr<'_>) -> bool {
118-
matches!(expr.kind, ExprKind::Unary(UnOp::Neg, _))
119-
}

clippy_utils/src/numeric_literal.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,14 @@ impl<'a> NumericLiteral<'a> {
5151
}
5252

5353
pub fn from_lit_kind(src: &'a str, lit_kind: &LitKind) -> Option<NumericLiteral<'a>> {
54-
if lit_kind.is_numeric() && src.chars().next().map_or(false, |c| c.is_digit(10)) {
54+
let unsigned_src = src.strip_prefix('-').map_or(src, |s| s);
55+
if lit_kind.is_numeric()
56+
&& unsigned_src
57+
.trim_start()
58+
.chars()
59+
.next()
60+
.map_or(false, |c| c.is_digit(10))
61+
{
5562
let (unsuffixed, suffix) = split_suffix(src, lit_kind);
5663
let float = matches!(lit_kind, LitKind::Float(..));
5764
Some(NumericLiteral::new(unsuffixed, suffix, float))

tests/ui/unnecessary_cast.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
#![warn(clippy::unnecessary_cast)]
22
#![allow(clippy::no_effect)]
33

4+
#[rustfmt::skip]
45
fn main() {
56
// Test cast_unnecessary
67
1i32 as i32;
78
1f32 as f32;
89
false as bool;
910
&1i32 as &i32;
1011

12+
-1_i32 as i32;
13+
- 1_i32 as i32;
14+
-1f32 as f32;
15+
1_i32 as i32;
16+
1_f32 as f32;
17+
1118
// macro version
1219
macro_rules! foo {
1320
($a:ident, $b:ident) => {

tests/ui/unnecessary_cast.stderr

+34-4
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,52 @@
11
error: casting integer literal to `i32` is unnecessary
2-
--> $DIR/unnecessary_cast.rs:6:5
2+
--> $DIR/unnecessary_cast.rs:7:5
33
|
44
LL | 1i32 as i32;
55
| ^^^^^^^^^^^ help: try: `1_i32`
66
|
77
= note: `-D clippy::unnecessary-cast` implied by `-D warnings`
88

99
error: casting float literal to `f32` is unnecessary
10-
--> $DIR/unnecessary_cast.rs:7:5
10+
--> $DIR/unnecessary_cast.rs:8:5
1111
|
1212
LL | 1f32 as f32;
1313
| ^^^^^^^^^^^ help: try: `1_f32`
1414

1515
error: casting to the same type is unnecessary (`bool` -> `bool`)
16-
--> $DIR/unnecessary_cast.rs:8:5
16+
--> $DIR/unnecessary_cast.rs:9:5
1717
|
1818
LL | false as bool;
1919
| ^^^^^^^^^^^^^ help: try: `false`
2020

21-
error: aborting due to 3 previous errors
21+
error: casting integer literal to `i32` is unnecessary
22+
--> $DIR/unnecessary_cast.rs:12:5
23+
|
24+
LL | -1_i32 as i32;
25+
| ^^^^^^^^^^^^^ help: try: `-1_i32`
26+
27+
error: casting integer literal to `i32` is unnecessary
28+
--> $DIR/unnecessary_cast.rs:13:5
29+
|
30+
LL | - 1_i32 as i32;
31+
| ^^^^^^^^^^^^^^ help: try: `- 1_i32`
32+
33+
error: casting float literal to `f32` is unnecessary
34+
--> $DIR/unnecessary_cast.rs:14:5
35+
|
36+
LL | -1f32 as f32;
37+
| ^^^^^^^^^^^^ help: try: `-1_f32`
38+
39+
error: casting integer literal to `i32` is unnecessary
40+
--> $DIR/unnecessary_cast.rs:15:5
41+
|
42+
LL | 1_i32 as i32;
43+
| ^^^^^^^^^^^^ help: try: `1_i32`
44+
45+
error: casting float literal to `f32` is unnecessary
46+
--> $DIR/unnecessary_cast.rs:16:5
47+
|
48+
LL | 1_f32 as f32;
49+
| ^^^^^^^^^^^^ help: try: `1_f32`
50+
51+
error: aborting due to 8 previous errors
2252

0 commit comments

Comments
 (0)