Skip to content

Commit fb1ae17

Browse files
committed
Fix cast_lossless false positive in impl const fn
Fixes #3656 (comment)
1 parent 28a8a6a commit fb1ae17

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

clippy_lints/src/utils/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ pub fn in_constant(cx: &LateContext<'_, '_>, id: HirId) -> bool {
8888
node: ItemKind::Fn(_, header, ..),
8989
..
9090
}) => header.constness == Constness::Const,
91+
Node::ImplItem(&ImplItem {
92+
node: ImplItemKind::Method(ref sig, _),
93+
..
94+
}) => sig.header.constness == Constness::Const,
9195
_ => false,
9296
}
9397
}

tests/ui/cast_lossless_float.fixed

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,14 @@ fn main() {
3232
const fn abc(input: f32) -> f64 {
3333
input as f64
3434
}
35+
36+
// Same as the above issue. We can't suggest `::from` in const fns in impls
37+
mod cast_lossless_in_impl {
38+
struct A;
39+
40+
impl A {
41+
pub const fn convert(x: f32) -> f64 {
42+
x as f64
43+
}
44+
}
45+
}

tests/ui/cast_lossless_float.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,14 @@ fn main() {
3232
const fn abc(input: f32) -> f64 {
3333
input as f64
3434
}
35+
36+
// Same as the above issue. We can't suggest `::from` in const fns in impls
37+
mod cast_lossless_in_impl {
38+
struct A;
39+
40+
impl A {
41+
pub const fn convert(x: f32) -> f64 {
42+
x as f64
43+
}
44+
}
45+
}

tests/ui/cast_lossless_integer.fixed

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,14 @@ fn main() {
3434
const fn abc(input: u16) -> u32 {
3535
input as u32
3636
}
37+
38+
// Same as the above issue. We can't suggest `::from` in const fns in impls
39+
mod cast_lossless_in_impl {
40+
struct A;
41+
42+
impl A {
43+
pub const fn convert(x: u32) -> u64 {
44+
x as u64
45+
}
46+
}
47+
}

tests/ui/cast_lossless_integer.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,14 @@ fn main() {
3434
const fn abc(input: u16) -> u32 {
3535
input as u32
3636
}
37+
38+
// Same as the above issue. We can't suggest `::from` in const fns in impls
39+
mod cast_lossless_in_impl {
40+
struct A;
41+
42+
impl A {
43+
pub const fn convert(x: u32) -> u64 {
44+
x as u64
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)