Skip to content

Commit 410d5ba

Browse files
committed
Auto merge of #3700 - phansch:would_you_like_some_help_with_this_const_fn, r=oli-obk
Prevent incorrect cast_lossless suggestion in const_fn `::from` is not a const fn, so applying the suggestion of `cast_lossless` would fail to compile. The fix is to skip the lint if the cast is found inside a const fn. Fixes #3656
2 parents 4a2116b + 8c416c3 commit 410d5ba

7 files changed

+72
-36
lines changed

clippy_lints/src/utils/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ pub fn in_constant(cx: &LateContext<'_, '_>, id: NodeId) -> bool {
8282
node: ItemKind::Static(..),
8383
..
8484
}) => true,
85+
Node::Item(&Item {
86+
node: ItemKind::Fn(_, header, ..),
87+
..
88+
}) => header.constness == Constness::Const,
8589
_ => false,
8690
}
8791
}

tests/ui/cast_lossless_float.fixed

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// run-rustfix
22

3-
#[warn(clippy::cast_lossless)]
4-
#[allow(clippy::no_effect, clippy::unnecessary_operation)]
3+
#![allow(clippy::no_effect, clippy::unnecessary_operation, dead_code)]
4+
#![warn(clippy::cast_lossless)]
5+
56
fn main() {
67
// Test clippy::cast_lossless with casts to floating-point types
78
f32::from(1i8);
@@ -15,3 +16,10 @@ fn main() {
1516
f64::from(1i32);
1617
f64::from(1u32);
1718
}
19+
20+
// The lint would suggest using `f64::from(input)` here but the `XX::from` function is not const,
21+
// so we skip the lint if the expression is in a const fn.
22+
// See #3656
23+
const fn abc(input: f32) -> f64 {
24+
input as f64
25+
}

tests/ui/cast_lossless_float.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// run-rustfix
22

3-
#[warn(clippy::cast_lossless)]
4-
#[allow(clippy::no_effect, clippy::unnecessary_operation)]
3+
#![allow(clippy::no_effect, clippy::unnecessary_operation, dead_code)]
4+
#![warn(clippy::cast_lossless)]
5+
56
fn main() {
67
// Test clippy::cast_lossless with casts to floating-point types
78
1i8 as f32;
@@ -15,3 +16,10 @@ fn main() {
1516
1i32 as f64;
1617
1u32 as f64;
1718
}
19+
20+
// The lint would suggest using `f64::from(input)` here but the `XX::from` function is not const,
21+
// so we skip the lint if the expression is in a const fn.
22+
// See #3656
23+
const fn abc(input: f32) -> f64 {
24+
input as f64
25+
}

tests/ui/cast_lossless_float.stderr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,61 @@
11
error: casting i8 to f32 may become silently lossy if types change
2-
--> $DIR/cast_lossless_float.rs:7:5
2+
--> $DIR/cast_lossless_float.rs:8:5
33
|
44
LL | 1i8 as f32;
55
| ^^^^^^^^^^ help: try: `f32::from(1i8)`
66
|
77
= note: `-D clippy::cast-lossless` implied by `-D warnings`
88

99
error: casting i8 to f64 may become silently lossy if types change
10-
--> $DIR/cast_lossless_float.rs:8:5
10+
--> $DIR/cast_lossless_float.rs:9:5
1111
|
1212
LL | 1i8 as f64;
1313
| ^^^^^^^^^^ help: try: `f64::from(1i8)`
1414

1515
error: casting u8 to f32 may become silently lossy if types change
16-
--> $DIR/cast_lossless_float.rs:9:5
16+
--> $DIR/cast_lossless_float.rs:10:5
1717
|
1818
LL | 1u8 as f32;
1919
| ^^^^^^^^^^ help: try: `f32::from(1u8)`
2020

2121
error: casting u8 to f64 may become silently lossy if types change
22-
--> $DIR/cast_lossless_float.rs:10:5
22+
--> $DIR/cast_lossless_float.rs:11:5
2323
|
2424
LL | 1u8 as f64;
2525
| ^^^^^^^^^^ help: try: `f64::from(1u8)`
2626

2727
error: casting i16 to f32 may become silently lossy if types change
28-
--> $DIR/cast_lossless_float.rs:11:5
28+
--> $DIR/cast_lossless_float.rs:12:5
2929
|
3030
LL | 1i16 as f32;
3131
| ^^^^^^^^^^^ help: try: `f32::from(1i16)`
3232

3333
error: casting i16 to f64 may become silently lossy if types change
34-
--> $DIR/cast_lossless_float.rs:12:5
34+
--> $DIR/cast_lossless_float.rs:13:5
3535
|
3636
LL | 1i16 as f64;
3737
| ^^^^^^^^^^^ help: try: `f64::from(1i16)`
3838

3939
error: casting u16 to f32 may become silently lossy if types change
40-
--> $DIR/cast_lossless_float.rs:13:5
40+
--> $DIR/cast_lossless_float.rs:14:5
4141
|
4242
LL | 1u16 as f32;
4343
| ^^^^^^^^^^^ help: try: `f32::from(1u16)`
4444

4545
error: casting u16 to f64 may become silently lossy if types change
46-
--> $DIR/cast_lossless_float.rs:14:5
46+
--> $DIR/cast_lossless_float.rs:15:5
4747
|
4848
LL | 1u16 as f64;
4949
| ^^^^^^^^^^^ help: try: `f64::from(1u16)`
5050

5151
error: casting i32 to f64 may become silently lossy if types change
52-
--> $DIR/cast_lossless_float.rs:15:5
52+
--> $DIR/cast_lossless_float.rs:16:5
5353
|
5454
LL | 1i32 as f64;
5555
| ^^^^^^^^^^^ help: try: `f64::from(1i32)`
5656

5757
error: casting u32 to f64 may become silently lossy if types change
58-
--> $DIR/cast_lossless_float.rs:16:5
58+
--> $DIR/cast_lossless_float.rs:17:5
5959
|
6060
LL | 1u32 as f64;
6161
| ^^^^^^^^^^^ help: try: `f64::from(1u32)`

tests/ui/cast_lossless_integer.fixed

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// run-rustfix
22

3-
#[warn(clippy::cast_lossless)]
4-
#[allow(clippy::no_effect, clippy::unnecessary_operation)]
3+
#![allow(clippy::no_effect, clippy::unnecessary_operation, dead_code)]
4+
#![warn(clippy::cast_lossless)]
5+
56
fn main() {
67
// Test clippy::cast_lossless with casts to integer types
78
i16::from(1i8);
@@ -23,3 +24,10 @@ fn main() {
2324
i64::from(1u32);
2425
u64::from(1u32);
2526
}
27+
28+
// The lint would suggest using `f64::from(input)` here but the `XX::from` function is not const,
29+
// so we skip the lint if the expression is in a const fn.
30+
// See #3656
31+
const fn abc(input: u16) -> u32 {
32+
input as u32
33+
}

tests/ui/cast_lossless_integer.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// run-rustfix
22

3-
#[warn(clippy::cast_lossless)]
4-
#[allow(clippy::no_effect, clippy::unnecessary_operation)]
3+
#![allow(clippy::no_effect, clippy::unnecessary_operation, dead_code)]
4+
#![warn(clippy::cast_lossless)]
5+
56
fn main() {
67
// Test clippy::cast_lossless with casts to integer types
78
1i8 as i16;
@@ -23,3 +24,10 @@ fn main() {
2324
1u32 as i64;
2425
1u32 as u64;
2526
}
27+
28+
// The lint would suggest using `f64::from(input)` here but the `XX::from` function is not const,
29+
// so we skip the lint if the expression is in a const fn.
30+
// See #3656
31+
const fn abc(input: u16) -> u32 {
32+
input as u32
33+
}

tests/ui/cast_lossless_integer.stderr

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,109 @@
11
error: casting i8 to i16 may become silently lossy if types change
2-
--> $DIR/cast_lossless_integer.rs:7:5
2+
--> $DIR/cast_lossless_integer.rs:8:5
33
|
44
LL | 1i8 as i16;
55
| ^^^^^^^^^^ help: try: `i16::from(1i8)`
66
|
77
= note: `-D clippy::cast-lossless` implied by `-D warnings`
88

99
error: casting i8 to i32 may become silently lossy if types change
10-
--> $DIR/cast_lossless_integer.rs:8:5
10+
--> $DIR/cast_lossless_integer.rs:9:5
1111
|
1212
LL | 1i8 as i32;
1313
| ^^^^^^^^^^ help: try: `i32::from(1i8)`
1414

1515
error: casting i8 to i64 may become silently lossy if types change
16-
--> $DIR/cast_lossless_integer.rs:9:5
16+
--> $DIR/cast_lossless_integer.rs:10:5
1717
|
1818
LL | 1i8 as i64;
1919
| ^^^^^^^^^^ help: try: `i64::from(1i8)`
2020

2121
error: casting u8 to i16 may become silently lossy if types change
22-
--> $DIR/cast_lossless_integer.rs:10:5
22+
--> $DIR/cast_lossless_integer.rs:11:5
2323
|
2424
LL | 1u8 as i16;
2525
| ^^^^^^^^^^ help: try: `i16::from(1u8)`
2626

2727
error: casting u8 to i32 may become silently lossy if types change
28-
--> $DIR/cast_lossless_integer.rs:11:5
28+
--> $DIR/cast_lossless_integer.rs:12:5
2929
|
3030
LL | 1u8 as i32;
3131
| ^^^^^^^^^^ help: try: `i32::from(1u8)`
3232

3333
error: casting u8 to i64 may become silently lossy if types change
34-
--> $DIR/cast_lossless_integer.rs:12:5
34+
--> $DIR/cast_lossless_integer.rs:13:5
3535
|
3636
LL | 1u8 as i64;
3737
| ^^^^^^^^^^ help: try: `i64::from(1u8)`
3838

3939
error: casting u8 to u16 may become silently lossy if types change
40-
--> $DIR/cast_lossless_integer.rs:13:5
40+
--> $DIR/cast_lossless_integer.rs:14:5
4141
|
4242
LL | 1u8 as u16;
4343
| ^^^^^^^^^^ help: try: `u16::from(1u8)`
4444

4545
error: casting u8 to u32 may become silently lossy if types change
46-
--> $DIR/cast_lossless_integer.rs:14:5
46+
--> $DIR/cast_lossless_integer.rs:15:5
4747
|
4848
LL | 1u8 as u32;
4949
| ^^^^^^^^^^ help: try: `u32::from(1u8)`
5050

5151
error: casting u8 to u64 may become silently lossy if types change
52-
--> $DIR/cast_lossless_integer.rs:15:5
52+
--> $DIR/cast_lossless_integer.rs:16:5
5353
|
5454
LL | 1u8 as u64;
5555
| ^^^^^^^^^^ help: try: `u64::from(1u8)`
5656

5757
error: casting i16 to i32 may become silently lossy if types change
58-
--> $DIR/cast_lossless_integer.rs:16:5
58+
--> $DIR/cast_lossless_integer.rs:17:5
5959
|
6060
LL | 1i16 as i32;
6161
| ^^^^^^^^^^^ help: try: `i32::from(1i16)`
6262

6363
error: casting i16 to i64 may become silently lossy if types change
64-
--> $DIR/cast_lossless_integer.rs:17:5
64+
--> $DIR/cast_lossless_integer.rs:18:5
6565
|
6666
LL | 1i16 as i64;
6767
| ^^^^^^^^^^^ help: try: `i64::from(1i16)`
6868

6969
error: casting u16 to i32 may become silently lossy if types change
70-
--> $DIR/cast_lossless_integer.rs:18:5
70+
--> $DIR/cast_lossless_integer.rs:19:5
7171
|
7272
LL | 1u16 as i32;
7373
| ^^^^^^^^^^^ help: try: `i32::from(1u16)`
7474

7575
error: casting u16 to i64 may become silently lossy if types change
76-
--> $DIR/cast_lossless_integer.rs:19:5
76+
--> $DIR/cast_lossless_integer.rs:20:5
7777
|
7878
LL | 1u16 as i64;
7979
| ^^^^^^^^^^^ help: try: `i64::from(1u16)`
8080

8181
error: casting u16 to u32 may become silently lossy if types change
82-
--> $DIR/cast_lossless_integer.rs:20:5
82+
--> $DIR/cast_lossless_integer.rs:21:5
8383
|
8484
LL | 1u16 as u32;
8585
| ^^^^^^^^^^^ help: try: `u32::from(1u16)`
8686

8787
error: casting u16 to u64 may become silently lossy if types change
88-
--> $DIR/cast_lossless_integer.rs:21:5
88+
--> $DIR/cast_lossless_integer.rs:22:5
8989
|
9090
LL | 1u16 as u64;
9191
| ^^^^^^^^^^^ help: try: `u64::from(1u16)`
9292

9393
error: casting i32 to i64 may become silently lossy if types change
94-
--> $DIR/cast_lossless_integer.rs:22:5
94+
--> $DIR/cast_lossless_integer.rs:23:5
9595
|
9696
LL | 1i32 as i64;
9797
| ^^^^^^^^^^^ help: try: `i64::from(1i32)`
9898

9999
error: casting u32 to i64 may become silently lossy if types change
100-
--> $DIR/cast_lossless_integer.rs:23:5
100+
--> $DIR/cast_lossless_integer.rs:24:5
101101
|
102102
LL | 1u32 as i64;
103103
| ^^^^^^^^^^^ help: try: `i64::from(1u32)`
104104

105105
error: casting u32 to u64 may become silently lossy if types change
106-
--> $DIR/cast_lossless_integer.rs:24:5
106+
--> $DIR/cast_lossless_integer.rs:25:5
107107
|
108108
LL | 1u32 as u64;
109109
| ^^^^^^^^^^^ help: try: `u64::from(1u32)`

0 commit comments

Comments
 (0)