From fb1ae1747f8a338ad4a69d5a8862f802ecf4de01 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Fri, 30 Aug 2019 07:22:35 +0200 Subject: [PATCH] Fix cast_lossless false positive in impl const fn Fixes https://github.com/rust-lang/rust-clippy/issues/3656#issuecomment-526387382 --- clippy_lints/src/utils/mod.rs | 4 ++++ tests/ui/cast_lossless_float.fixed | 11 +++++++++++ tests/ui/cast_lossless_float.rs | 11 +++++++++++ tests/ui/cast_lossless_integer.fixed | 11 +++++++++++ tests/ui/cast_lossless_integer.rs | 11 +++++++++++ 5 files changed, 48 insertions(+) diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index 8fb45899653c..1aff61e31860 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -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, } } diff --git a/tests/ui/cast_lossless_float.fixed b/tests/ui/cast_lossless_float.fixed index 8021dc229e5e..709d58b596c8 100644 --- a/tests/ui/cast_lossless_float.fixed +++ b/tests/ui/cast_lossless_float.fixed @@ -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 + } + } +} diff --git a/tests/ui/cast_lossless_float.rs b/tests/ui/cast_lossless_float.rs index 3cd5ad622036..eb0aab886429 100644 --- a/tests/ui/cast_lossless_float.rs +++ b/tests/ui/cast_lossless_float.rs @@ -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 + } + } +} diff --git a/tests/ui/cast_lossless_integer.fixed b/tests/ui/cast_lossless_integer.fixed index 22936e41c909..03e49adb117d 100644 --- a/tests/ui/cast_lossless_integer.fixed +++ b/tests/ui/cast_lossless_integer.fixed @@ -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 + } + } +} diff --git a/tests/ui/cast_lossless_integer.rs b/tests/ui/cast_lossless_integer.rs index 958a336cf2cd..6a984d245963 100644 --- a/tests/ui/cast_lossless_integer.rs +++ b/tests/ui/cast_lossless_integer.rs @@ -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 + } + } +}