2018-08-27 13:49:54 +00:00
|
|
|
|
use crate::utils;
|
2018-12-29 15:04:45 +00:00
|
|
|
|
use rustc::{declare_tool_lint, hir, lint, lint_array};
|
|
|
|
|
use rustc_errors::Applicability;
|
2018-08-29 12:40:00 +00:00
|
|
|
|
use std::fmt;
|
2018-08-27 13:49:54 +00:00
|
|
|
|
|
|
|
|
|
/// **What it does:** Checks for usage of the `offset` pointer method with a `usize` casted to an
|
|
|
|
|
/// `isize`.
|
|
|
|
|
///
|
|
|
|
|
/// **Why is this bad?** If we’re always increasing the pointer address, we can avoid the numeric
|
|
|
|
|
/// cast by using the `add` method instead.
|
|
|
|
|
///
|
|
|
|
|
/// **Known problems:** None
|
|
|
|
|
///
|
|
|
|
|
/// **Example:**
|
|
|
|
|
/// ```rust
|
|
|
|
|
/// let vec = vec![b'a', b'b', b'c'];
|
|
|
|
|
/// let ptr = vec.as_ptr();
|
|
|
|
|
/// let offset = 1_usize;
|
|
|
|
|
///
|
2018-11-27 20:14:15 +00:00
|
|
|
|
/// unsafe {
|
|
|
|
|
/// ptr.offset(offset as isize);
|
|
|
|
|
/// }
|
2018-08-27 13:49:54 +00:00
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// Could be written:
|
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
|
|
|
|
/// let vec = vec![b'a', b'b', b'c'];
|
|
|
|
|
/// let ptr = vec.as_ptr();
|
|
|
|
|
/// let offset = 1_usize;
|
|
|
|
|
///
|
2018-11-27 20:14:15 +00:00
|
|
|
|
/// unsafe {
|
|
|
|
|
/// ptr.add(offset);
|
|
|
|
|
/// }
|
2018-08-27 13:49:54 +00:00
|
|
|
|
/// ```
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
|
pub PTR_OFFSET_WITH_CAST,
|
2018-08-29 12:02:26 +00:00
|
|
|
|
complexity,
|
2018-10-18 21:31:11 +00:00
|
|
|
|
"unneeded pointer offset cast"
|
2018-08-27 13:49:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
|
pub struct Pass;
|
|
|
|
|
|
|
|
|
|
impl lint::LintPass for Pass {
|
|
|
|
|
fn get_lints(&self) -> lint::LintArray {
|
|
|
|
|
lint_array!(PTR_OFFSET_WITH_CAST)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, 'tcx> lint::LateLintPass<'a, 'tcx> for Pass {
|
|
|
|
|
fn check_expr(&mut self, cx: &lint::LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
|
2018-08-29 12:40:00 +00:00
|
|
|
|
// Check if the expressions is a ptr.offset or ptr.wrapping_offset method call
|
|
|
|
|
let (receiver_expr, arg_expr, method) = match expr_as_ptr_offset_call(cx, expr) {
|
2018-08-27 13:49:54 +00:00
|
|
|
|
Some(call_arg) => call_arg,
|
|
|
|
|
None => return,
|
|
|
|
|
};
|
|
|
|
|
|
2018-08-29 12:40:00 +00:00
|
|
|
|
// Check if the argument to the method call is a cast from usize
|
2018-08-27 13:49:54 +00:00
|
|
|
|
let cast_lhs_expr = match expr_as_cast_from_usize(cx, arg_expr) {
|
|
|
|
|
Some(cast_lhs_expr) => cast_lhs_expr,
|
|
|
|
|
None => return,
|
|
|
|
|
};
|
|
|
|
|
|
2018-08-29 12:40:00 +00:00
|
|
|
|
let msg = format!("use of `{}` with a `usize` casted to an `isize`", method);
|
|
|
|
|
if let Some(sugg) = build_suggestion(cx, method, receiver_expr, cast_lhs_expr) {
|
2018-11-20 13:06:29 +00:00
|
|
|
|
utils::span_lint_and_sugg(
|
|
|
|
|
cx,
|
|
|
|
|
PTR_OFFSET_WITH_CAST,
|
|
|
|
|
expr.span,
|
|
|
|
|
&msg,
|
|
|
|
|
"try",
|
|
|
|
|
sugg,
|
2018-11-27 14:13:57 +00:00
|
|
|
|
Applicability::MachineApplicable,
|
2018-11-20 13:06:29 +00:00
|
|
|
|
);
|
2018-08-29 12:12:22 +00:00
|
|
|
|
} else {
|
2018-08-29 12:40:00 +00:00
|
|
|
|
utils::span_lint(cx, PTR_OFFSET_WITH_CAST, expr.span, &msg);
|
2018-08-29 12:12:22 +00:00
|
|
|
|
}
|
2018-08-27 13:49:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the given expression is a cast from a usize, return the lhs of the cast
|
|
|
|
|
fn expr_as_cast_from_usize<'a, 'tcx>(
|
|
|
|
|
cx: &lint::LateContext<'a, 'tcx>,
|
|
|
|
|
expr: &'tcx hir::Expr,
|
|
|
|
|
) -> Option<&'tcx hir::Expr> {
|
|
|
|
|
if let hir::ExprKind::Cast(ref cast_lhs_expr, _) = expr.node {
|
|
|
|
|
if is_expr_ty_usize(cx, &cast_lhs_expr) {
|
|
|
|
|
return Some(cast_lhs_expr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-29 12:40:00 +00:00
|
|
|
|
// If the given expression is a ptr::offset or ptr::wrapping_offset method call, return the
|
|
|
|
|
// receiver, the arg of the method call, and the method.
|
2018-08-27 13:49:54 +00:00
|
|
|
|
fn expr_as_ptr_offset_call<'a, 'tcx>(
|
|
|
|
|
cx: &lint::LateContext<'a, 'tcx>,
|
|
|
|
|
expr: &'tcx hir::Expr,
|
2018-08-29 12:40:00 +00:00
|
|
|
|
) -> Option<(&'tcx hir::Expr, &'tcx hir::Expr, Method)> {
|
2018-08-27 13:49:54 +00:00
|
|
|
|
if let hir::ExprKind::MethodCall(ref path_segment, _, ref args) = expr.node {
|
2018-08-29 12:40:00 +00:00
|
|
|
|
if is_expr_ty_raw_ptr(cx, &args[0]) {
|
|
|
|
|
if path_segment.ident.name == "offset" {
|
|
|
|
|
return Some((&args[0], &args[1], Method::Offset));
|
|
|
|
|
}
|
|
|
|
|
if path_segment.ident.name == "wrapping_offset" {
|
|
|
|
|
return Some((&args[0], &args[1], Method::WrappingOffset));
|
|
|
|
|
}
|
2018-08-27 13:49:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Is the type of the expression a usize?
|
2018-11-27 20:14:15 +00:00
|
|
|
|
fn is_expr_ty_usize<'a, 'tcx>(cx: &lint::LateContext<'a, 'tcx>, expr: &hir::Expr) -> bool {
|
2018-08-29 12:07:23 +00:00
|
|
|
|
cx.tables.expr_ty(expr) == cx.tcx.types.usize
|
2018-08-27 13:49:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Is the type of the expression a raw pointer?
|
2018-11-27 20:14:15 +00:00
|
|
|
|
fn is_expr_ty_raw_ptr<'a, 'tcx>(cx: &lint::LateContext<'a, 'tcx>, expr: &hir::Expr) -> bool {
|
2018-08-29 12:08:59 +00:00
|
|
|
|
cx.tables.expr_ty(expr).is_unsafe_ptr()
|
2018-08-27 13:49:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn build_suggestion<'a, 'tcx>(
|
|
|
|
|
cx: &lint::LateContext<'a, 'tcx>,
|
2018-08-29 12:40:00 +00:00
|
|
|
|
method: Method,
|
2018-08-27 13:49:54 +00:00
|
|
|
|
receiver_expr: &hir::Expr,
|
|
|
|
|
cast_lhs_expr: &hir::Expr,
|
2018-08-29 12:12:22 +00:00
|
|
|
|
) -> Option<String> {
|
2018-08-29 12:42:43 +00:00
|
|
|
|
let receiver = utils::snippet_opt(cx, receiver_expr.span)?;
|
|
|
|
|
let cast_lhs = utils::snippet_opt(cx, cast_lhs_expr.span)?;
|
|
|
|
|
Some(format!("{}.{}({})", receiver, method.suggestion(), cast_lhs))
|
2018-08-27 13:49:54 +00:00
|
|
|
|
}
|
2018-08-29 12:40:00 +00:00
|
|
|
|
|
2018-08-29 13:01:05 +00:00
|
|
|
|
#[derive(Copy, Clone)]
|
2018-08-29 12:40:00 +00:00
|
|
|
|
enum Method {
|
|
|
|
|
Offset,
|
|
|
|
|
WrappingOffset,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Method {
|
2018-08-29 13:27:32 +00:00
|
|
|
|
fn suggestion(self) -> &'static str {
|
2018-08-29 13:59:38 +00:00
|
|
|
|
match self {
|
2018-08-29 12:40:00 +00:00
|
|
|
|
Method::Offset => "add",
|
|
|
|
|
Method::WrappingOffset => "wrapping_add",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for Method {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-08-29 13:59:38 +00:00
|
|
|
|
match self {
|
2018-08-29 12:40:00 +00:00
|
|
|
|
Method::Offset => write!(f, "offset"),
|
|
|
|
|
Method::WrappingOffset => write!(f, "wrapping_offset"),
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-29 12:43:40 +00:00
|
|
|
|
}
|