move zst_offset to its own module

This commit is contained in:
Takayuki Maeda 2021-03-04 19:46:44 +09:00
parent 6376da70be
commit 9b0a42b67d
2 changed files with 21 additions and 13 deletions

View File

@ -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]);

View File

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