use std::fmt;
declare_clippy_lint! {
- /// **What it does:** Checks for usage of the `offset` pointer method with a `usize` casted to an
+ /// ### 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
+ /// ### 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:**
+ /// ### Example
/// ```rust
/// let vec = vec![b'a', b'b', b'c'];
/// let ptr = vec.as_ptr();
/// ptr.add(offset);
/// }
/// ```
+ #[clippy::version = "1.30.0"]
pub PTR_OFFSET_WITH_CAST,
complexity,
"unneeded pointer offset cast"
None => return,
};
- let msg = format!("use of `{}` with a `usize` casted to an `isize`", method);
+ let msg = format!("use of `{method}` with a `usize` casted to an `isize`");
if let Some(sugg) = build_suggestion(cx, method, receiver_expr, cast_lhs_expr) {
span_lint_and_sugg(
cx,
cx: &LateContext<'tcx>,
expr: &'tcx Expr<'_>,
) -> Option<(&'tcx Expr<'tcx>, &'tcx Expr<'tcx>, Method)> {
- if let ExprKind::MethodCall(path_segment, _, args, _) = expr.kind {
- if is_expr_ty_raw_ptr(cx, &args[0]) {
+ if let ExprKind::MethodCall(path_segment, arg_0, [arg_1, ..], _) = &expr.kind {
+ if is_expr_ty_raw_ptr(cx, arg_0) {
if path_segment.ident.name == sym::offset {
- return Some((&args[0], &args[1], Method::Offset));
+ return Some((arg_0, arg_1, Method::Offset));
}
if path_segment.ident.name == sym!(wrapping_offset) {
- return Some((&args[0], &args[1], Method::WrappingOffset));
+ return Some((arg_0, arg_1, Method::WrappingOffset));
}
}
}
) -> Option<String> {
let receiver = snippet_opt(cx, receiver_expr.span)?;
let cast_lhs = snippet_opt(cx, cast_lhs_expr.span)?;
- Some(format!("{}.{}({})", receiver, method.suggestion(), cast_lhs))
+ Some(format!("{receiver}.{}({cast_lhs})", method.suggestion()))
}
#[derive(Copy, Clone)]