Skip to content

Commit

Permalink
move zst_offset to its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
TaKO8Ki committed Mar 2, 2021
1 parent 84ab59c commit 81d4dbe
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
15 changes: 2 additions & 13 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"] => {
Expand Down Expand Up @@ -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]);
Expand Down
19 changes: 19 additions & 0 deletions clippy_lints/src/methods/zst_offset.rs
Original file line number Diff line number Diff line change
@@ -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::{self};

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");
}
}
}

0 comments on commit 81d4dbe

Please sign in to comment.