about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--clippy_lints/src/methods/mod.rs15
-rw-r--r--clippy_lints/src/methods/zst_offset.rs19
2 files changed, 21 insertions, 13 deletions
diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs
index 639d013f786..c944568dc61 100644
--- a/clippy_lints/src/methods/mod.rs
+++ b/clippy_lints/src/methods/mod.rs
@@ -20,6 +20,7 @@ mod uninit_assumed_init;
 mod unnecessary_filter_map;
 mod unnecessary_lazy_eval;
 mod unwrap_used;
+mod zst_offset;
 
 use std::borrow::Cow;
 use std::fmt;
@@ -1726,7 +1727,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
                 manual_saturating_arithmetic::check(cx, expr, &arg_lists, &arith["checked_".len()..])
             },
             ["add" | "offset" | "sub" | "wrapping_offset" | "wrapping_add" | "wrapping_sub"] => {
-                check_pointer_offset(cx, expr, arg_lists[0])
+                zst_offset::check(cx, expr, arg_lists[0])
             },
             ["is_file", ..] => filetype_is_file::check(cx, expr, arg_lists[0]),
             ["map", "as_ref"] => {
@@ -3801,18 +3802,6 @@ fn is_bool(ty: &hir::Ty<'_>) -> bool {
     }
 }
 
-fn check_pointer_offset(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
-    if_chain! {
-        if args.len() == 2;
-        if let ty::RawPtr(ty::TypeAndMut { ref ty, .. }) = cx.typeck_results().expr_ty(&args[0]).kind();
-        if let Ok(layout) = cx.tcx.layout_of(cx.param_env.and(ty));
-        if layout.is_zst();
-        then {
-            span_lint(cx, ZST_OFFSET, expr.span, "offset calculation on zero-sized value");
-        }
-    }
-}
-
 fn lint_from_iter(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
     let ty = cx.typeck_results().expr_ty(expr);
     let arg_ty = cx.typeck_results().expr_ty(&args[0]);
diff --git a/clippy_lints/src/methods/zst_offset.rs b/clippy_lints/src/methods/zst_offset.rs
new file mode 100644
index 00000000000..f1335726736
--- /dev/null
+++ b/clippy_lints/src/methods/zst_offset.rs
@@ -0,0 +1,19 @@
+use crate::utils::span_lint;
+use if_chain::if_chain;
+use rustc_hir as hir;
+use rustc_lint::LateContext;
+use rustc_middle::ty;
+
+use super::ZST_OFFSET;
+
+pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
+    if_chain! {
+        if args.len() == 2;
+        if let ty::RawPtr(ty::TypeAndMut { ref ty, .. }) = cx.typeck_results().expr_ty(&args[0]).kind();
+        if let Ok(layout) = cx.tcx.layout_of(cx.param_env.and(ty));
+        if layout.is_zst();
+        then {
+            span_lint(cx, ZST_OFFSET, expr.span, "offset calculation on zero-sized value");
+        }
+    }
+}