Skip to content

Commit

Permalink
Auto merge of #4473 - phansch:fix_cast_lossless_fp, r=flip1995
Browse files Browse the repository at this point in the history
Fix cast_lossless false positive in impl const fn

Fixes #3656 (comment)

changelog: Fix false positive in `cast_lossless`
  • Loading branch information
bors committed Aug 30, 2019
2 parents 70e7d07 + fb1ae17 commit a3fcaee
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions clippy_lints/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ pub fn in_constant(cx: &LateContext<'_, '_>, id: HirId) -> bool {
node: ItemKind::Fn(_, header, ..),
..
}) => header.constness == Constness::Const,
Node::ImplItem(&ImplItem {
node: ImplItemKind::Method(ref sig, _),
..
}) => sig.header.constness == Constness::Const,
_ => false,
}
}
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/cast_lossless_float.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,14 @@ fn main() {
const fn abc(input: f32) -> f64 {
input as f64
}

// Same as the above issue. We can't suggest `::from` in const fns in impls
mod cast_lossless_in_impl {
struct A;

impl A {
pub const fn convert(x: f32) -> f64 {
x as f64
}
}
}
11 changes: 11 additions & 0 deletions tests/ui/cast_lossless_float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,14 @@ fn main() {
const fn abc(input: f32) -> f64 {
input as f64
}

// Same as the above issue. We can't suggest `::from` in const fns in impls
mod cast_lossless_in_impl {
struct A;

impl A {
pub const fn convert(x: f32) -> f64 {
x as f64
}
}
}
11 changes: 11 additions & 0 deletions tests/ui/cast_lossless_integer.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,14 @@ fn main() {
const fn abc(input: u16) -> u32 {
input as u32
}

// Same as the above issue. We can't suggest `::from` in const fns in impls
mod cast_lossless_in_impl {
struct A;

impl A {
pub const fn convert(x: u32) -> u64 {
x as u64
}
}
}
11 changes: 11 additions & 0 deletions tests/ui/cast_lossless_integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,14 @@ fn main() {
const fn abc(input: u16) -> u32 {
input as u32
}

// Same as the above issue. We can't suggest `::from` in const fns in impls
mod cast_lossless_in_impl {
struct A;

impl A {
pub const fn convert(x: u32) -> u64 {
x as u64
}
}
}

0 comments on commit a3fcaee

Please sign in to comment.