about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMarcin Serwin <toxyxer@gmail.com>2020-03-19 15:53:02 +0100
committerMarcin Serwin <toxyxer@gmail.com>2020-04-09 08:05:51 +0200
commit2153abb4124fd3dca018d4adb4e79693f1a9fedd (patch)
treef4a1275a38ed3847d223a3a775e73dbec11f3f1b
parentb4ff774d723baa39de0df43a7e87b1b6a1df5a5a (diff)
downloadrust-2153abb4124fd3dca018d4adb4e79693f1a9fedd.tar.gz
rust-2153abb4124fd3dca018d4adb4e79693f1a9fedd.zip
Add handling of float arrays to miri_to_const
-rw-r--r--clippy_lints/src/consts.rs35
1 files changed, 35 insertions, 0 deletions
diff --git a/clippy_lints/src/consts.rs b/clippy_lints/src/consts.rs
index b1d540c9751..c64c00134e8 100644
--- a/clippy_lints/src/consts.rs
+++ b/clippy_lints/src/consts.rs
@@ -492,6 +492,41 @@ pub fn miri_to_const(result: &ty::Const<'_>) -> Option<Constant> {
             },
             _ => None,
         },
+        ty::ConstKind::Value(ConstValue::ByRef { alloc, offset: _ }) => match result.ty.kind {
+            ty::Array(sub_type, len) => match sub_type.kind {
+                ty::Float(FloatTy::F32) => match miri_to_const(len) {
+                    Some(Constant::Int(len)) => alloc
+                        .inspect_with_undef_and_ptr_outside_interpreter(0..(4 * len as usize))
+                        .to_owned()
+                        .chunks(4)
+                        .map(|chunk| {
+                            Some(Constant::F32(f32::from_le_bytes(
+                                chunk.try_into().expect("this shouldn't happen"),
+                            )))
+                        })
+                        .collect::<Option<Vec<Constant>>>()
+                        .map(Constant::Vec),
+                    _ => None,
+                },
+                ty::Float(FloatTy::F64) => match miri_to_const(len) {
+                    Some(Constant::Int(len)) => alloc
+                        .inspect_with_undef_and_ptr_outside_interpreter(0..(8 * len as usize))
+                        .to_owned()
+                        .chunks(8)
+                        .map(|chunk| {
+                            Some(Constant::F64(f64::from_le_bytes(
+                                chunk.try_into().expect("this shouldn't happen"),
+                            )))
+                        })
+                        .collect::<Option<Vec<Constant>>>()
+                        .map(Constant::Vec),
+                    _ => None,
+                },
+                // FIXME: implement other array type conversions.
+                _ => None,
+            },
+            _ => None,
+        },
         // FIXME: implement other conversions.
         _ => None,
     }