]> git.lizzy.rs Git - rust.git/blobdiff - clippy_utils/src/consts.rs
add eq constraints on associated constants
[rust.git] / clippy_utils / src / consts.rs
index 04347672e0fbb35e12241580aacfeac22de28fbc..e09a663538ddcfeff5899eae3d0af5155b6a2340 100644 (file)
@@ -168,6 +168,14 @@ pub fn int_value(&self, cx: &LateContext<'_>, val_type: Ty<'_>) -> Option<FullIn
             None
         }
     }
+
+    #[must_use]
+    pub fn peel_refs(mut self) -> Self {
+        while let Constant::Ref(r) = self {
+            self = *r;
+        }
+        self
+    }
 }
 
 /// Parses a `LitKind` to a `Constant`.
@@ -215,7 +223,7 @@ pub fn constant_simple<'tcx>(
     constant(lcx, typeck_results, e).and_then(|(cst, res)| if res { None } else { Some(cst) })
 }
 
-pub fn constant_full_int(
+pub fn constant_full_int<'tcx>(
     lcx: &LateContext<'tcx>,
     typeck_results: &ty::TypeckResults<'tcx>,
     e: &Expr<'_>,
@@ -319,8 +327,8 @@ pub fn expr(&mut self, e: &Expr<'_>) -> Option<Constant> {
                     if let ExprKind::Path(qpath) = &callee.kind;
                     let res = self.typeck_results.qpath_res(qpath, callee.hir_id);
                     if let Some(def_id) = res.opt_def_id();
-                    let def_path: Vec<_> = self.lcx.get_def_path(def_id).into_iter().map(Symbol::as_str).collect();
-                    let def_path: Vec<&str> = def_path.iter().take(4).map(|s| &**s).collect();
+                    let def_path = self.lcx.get_def_path(def_id);
+                    let def_path: Vec<&str> = def_path.iter().take(4).map(Symbol::as_str).collect();
                     if let ["core", "num", int_impl, "max_value"] = *def_path;
                     then {
                        let value = match int_impl {
@@ -405,7 +413,10 @@ fn fetch_path(&mut self, qpath: &QPath<'_>, id: HirId, ty: Ty<'tcx>) -> Option<C
                     .tcx
                     .const_eval_resolve(
                         self.param_env,
-                        ty::Unevaluated::new(ty::WithOptConstParam::unknown(def_id), substs),
+                        ty::Unevaluated::new(
+                            ty::WithOptConstParam::unknown(def_id),
+                            substs,
+                        ),
                         None,
                     )
                     .ok()