From 5ebae01c1eb7beafb455d432bd4d4f8f612ead2a Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Mon, 27 Aug 2018 09:49:54 -0400 Subject: [PATCH 01/12] New lint: Suggest `ptr.add([usize])` over `ptr.offset([usize] as isize)`. First part of #3047. --- clippy_lints/src/lib.rs | 4 + clippy_lints/src/ptr_offset_with_cast.rs | 130 +++++++++++++++++++++++ tests/ui/ptr_offset_with_cast.rs | 14 +++ tests/ui/ptr_offset_with_cast.stderr | 10 ++ 4 files changed, 158 insertions(+) create mode 100644 clippy_lints/src/ptr_offset_with_cast.rs create mode 100644 tests/ui/ptr_offset_with_cast.rs create mode 100644 tests/ui/ptr_offset_with_cast.stderr diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 84db58b8971..ccb849a0c06 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -142,6 +142,7 @@ pub mod panic_unimplemented; pub mod partialeq_ne_impl; pub mod precedence; pub mod ptr; +pub mod ptr_offset_with_cast; pub mod question_mark; pub mod ranges; pub mod redundant_field_names; @@ -408,6 +409,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { reg.register_late_lint_pass(box default_trait_access::DefaultTraitAccess); reg.register_late_lint_pass(box indexing_slicing::IndexingSlicing); reg.register_late_lint_pass(box non_copy_const::NonCopyConst); + reg.register_late_lint_pass(box ptr_offset_with_cast::Pass); reg.register_lint_group("clippy_restriction", vec![ arithmetic::FLOAT_ARITHMETIC, @@ -631,6 +633,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { ptr::CMP_NULL, ptr::MUT_FROM_REF, ptr::PTR_ARG, + ptr_offset_with_cast::PTR_OFFSET_WITH_CAST, question_mark::QUESTION_MARK, ranges::ITERATOR_STEP_BY_ZERO, ranges::RANGE_MINUS_ONE, @@ -755,6 +758,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { panic_unimplemented::PANIC_PARAMS, ptr::CMP_NULL, ptr::PTR_ARG, + ptr_offset_with_cast::PTR_OFFSET_WITH_CAST, question_mark::QUESTION_MARK, redundant_field_names::REDUNDANT_FIELD_NAMES, regex::REGEX_MACRO, diff --git a/clippy_lints/src/ptr_offset_with_cast.rs b/clippy_lints/src/ptr_offset_with_cast.rs new file mode 100644 index 00000000000..4a6bc8215ef --- /dev/null +++ b/clippy_lints/src/ptr_offset_with_cast.rs @@ -0,0 +1,130 @@ +use rustc::{declare_lint, hir, lint, lint_array, ty}; +use syntax::ast; +use crate::utils; + +/// **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; +/// +/// unsafe { ptr.offset(offset as isize); } +/// ``` +/// +/// Could be written: +/// +/// ```rust +/// let vec = vec![b'a', b'b', b'c']; +/// let ptr = vec.as_ptr(); +/// let offset = 1_usize; +/// +/// unsafe { ptr.add(offset); } +/// ``` +declare_clippy_lint! { + pub PTR_OFFSET_WITH_CAST, + style, + "uneeded pointer offset cast" +} + +#[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) { + // Check if the expressions is a ptr.offset method call + let [receiver_expr, arg_expr] = match expr_as_ptr_offset_call(cx, expr) { + Some(call_arg) => call_arg, + None => return, + }; + + // Check if the parameter to ptr.offset is a cast from usize to isize + let cast_lhs_expr = match expr_as_cast_from_usize(cx, arg_expr) { + Some(cast_lhs_expr) => cast_lhs_expr, + None => return, + }; + + utils::span_lint_and_sugg( + cx, + PTR_OFFSET_WITH_CAST, + expr.span, + "use of `offset` with a `usize` casted to an `isize`", + "try", + build_suggestion(cx, receiver_expr, cast_lhs_expr), + ); + } +} + +// 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 +} + +// If the given expression is a ptr::offset method call, return the receiver and the arg of the +// method call. +fn expr_as_ptr_offset_call<'a, 'tcx>( + cx: &lint::LateContext<'a, 'tcx>, + expr: &'tcx hir::Expr, +) -> Option<[&'tcx hir::Expr; 2]> { + if let hir::ExprKind::MethodCall(ref path_segment, _, ref args) = expr.node { + if path_segment.ident.name == "offset" && is_expr_ty_raw_ptr(cx, &args[0]) { + return Some([&args[0], &args[1]]); + } + } + None +} + +// Is the type of the expression a usize? +fn is_expr_ty_usize<'a, 'tcx>( + cx: &lint::LateContext<'a, 'tcx>, + expr: &hir::Expr, +) -> bool { + cx.tables.expr_ty(expr).sty == ty::TyKind::Uint(ast::UintTy::Usize) +} + +// Is the type of the expression a raw pointer? +fn is_expr_ty_raw_ptr<'a, 'tcx>( + cx: &lint::LateContext<'a, 'tcx>, + expr: &hir::Expr, +) -> bool { + if let ty::RawPtr(..) = cx.tables.expr_ty(expr).sty { + true + } else { + false + } +} + +fn build_suggestion<'a, 'tcx>( + cx: &lint::LateContext<'a, 'tcx>, + receiver_expr: &hir::Expr, + cast_lhs_expr: &hir::Expr, +) -> String { + match ( + utils::snippet_opt(cx, receiver_expr.span), + utils::snippet_opt(cx, cast_lhs_expr.span) + ) { + (Some(receiver), Some(cast_lhs)) => format!("{}.add({})", receiver, cast_lhs), + _ => String::new(), + } +} diff --git a/tests/ui/ptr_offset_with_cast.rs b/tests/ui/ptr_offset_with_cast.rs new file mode 100644 index 00000000000..947021aaf8e --- /dev/null +++ b/tests/ui/ptr_offset_with_cast.rs @@ -0,0 +1,14 @@ +fn main() { + let vec = vec![b'a', b'b', b'c']; + let ptr = vec.as_ptr(); + + let offset_u8 = 1_u8; + let offset_usize = 1_usize; + let offset_isize = 1_isize; + + unsafe { + ptr.offset(offset_usize as isize); + ptr.offset(offset_isize as isize); + ptr.offset(offset_u8 as isize); + } +} diff --git a/tests/ui/ptr_offset_with_cast.stderr b/tests/ui/ptr_offset_with_cast.stderr new file mode 100644 index 00000000000..1658f5f5366 --- /dev/null +++ b/tests/ui/ptr_offset_with_cast.stderr @@ -0,0 +1,10 @@ +error: use of `offset` with a `usize` casted to an `isize` + --> $DIR/ptr_offset_with_cast.rs:10:9 + | +10 | ptr.offset(offset_usize as isize); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr.add(offset_usize)` + | + = note: `-D ptr-offset-with-cast` implied by `-D warnings` + +error: aborting due to previous error + From feb3e9fd5f8b24bcbe1a470b65dac66acb265721 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Wed, 29 Aug 2018 07:02:26 -0500 Subject: [PATCH 02/12] switch lint from 'style' to 'complexity' --- clippy_lints/src/lib.rs | 2 +- clippy_lints/src/ptr_offset_with_cast.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index ccb849a0c06..7d429ce4923 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -758,7 +758,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { panic_unimplemented::PANIC_PARAMS, ptr::CMP_NULL, ptr::PTR_ARG, - ptr_offset_with_cast::PTR_OFFSET_WITH_CAST, question_mark::QUESTION_MARK, redundant_field_names::REDUNDANT_FIELD_NAMES, regex::REGEX_MACRO, @@ -819,6 +818,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL, partialeq_ne_impl::PARTIALEQ_NE_IMPL, precedence::PRECEDENCE, + ptr_offset_with_cast::PTR_OFFSET_WITH_CAST, ranges::RANGE_MINUS_ONE, ranges::RANGE_PLUS_ONE, ranges::RANGE_ZIP_WITH_LEN, diff --git a/clippy_lints/src/ptr_offset_with_cast.rs b/clippy_lints/src/ptr_offset_with_cast.rs index 4a6bc8215ef..9c8382e43f2 100644 --- a/clippy_lints/src/ptr_offset_with_cast.rs +++ b/clippy_lints/src/ptr_offset_with_cast.rs @@ -30,7 +30,7 @@ use crate::utils; /// ``` declare_clippy_lint! { pub PTR_OFFSET_WITH_CAST, - style, + complexity, "uneeded pointer offset cast" } From a7c1ea96c4a6dc3c6614331124200ac4c5588b6b Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Wed, 29 Aug 2018 07:03:50 -0500 Subject: [PATCH 03/12] tweak comment --- clippy_lints/src/ptr_offset_with_cast.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clippy_lints/src/ptr_offset_with_cast.rs b/clippy_lints/src/ptr_offset_with_cast.rs index 9c8382e43f2..756f8dfe2e5 100644 --- a/clippy_lints/src/ptr_offset_with_cast.rs +++ b/clippy_lints/src/ptr_offset_with_cast.rs @@ -51,7 +51,7 @@ impl<'a, 'tcx> lint::LateLintPass<'a, 'tcx> for Pass { None => return, }; - // Check if the parameter to ptr.offset is a cast from usize to isize + // Check if the argument to ptr.offset is a cast from usize let cast_lhs_expr = match expr_as_cast_from_usize(cx, arg_expr) { Some(cast_lhs_expr) => cast_lhs_expr, None => return, From a48a6ef10f76b02a88927198f4e06d7e81b89027 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Wed, 29 Aug 2018 07:07:23 -0500 Subject: [PATCH 04/12] utilize cx.tcx.types.usize --- clippy_lints/src/ptr_offset_with_cast.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/clippy_lints/src/ptr_offset_with_cast.rs b/clippy_lints/src/ptr_offset_with_cast.rs index 756f8dfe2e5..af9989a161c 100644 --- a/clippy_lints/src/ptr_offset_with_cast.rs +++ b/clippy_lints/src/ptr_offset_with_cast.rs @@ -1,5 +1,4 @@ use rustc::{declare_lint, hir, lint, lint_array, ty}; -use syntax::ast; use crate::utils; /// **What it does:** Checks for usage of the `offset` pointer method with a `usize` casted to an @@ -100,7 +99,7 @@ fn is_expr_ty_usize<'a, 'tcx>( cx: &lint::LateContext<'a, 'tcx>, expr: &hir::Expr, ) -> bool { - cx.tables.expr_ty(expr).sty == ty::TyKind::Uint(ast::UintTy::Usize) + cx.tables.expr_ty(expr) == cx.tcx.types.usize } // Is the type of the expression a raw pointer? From f7d1ef90a04ad40148dc0db4ccf739e8178b11b1 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Wed, 29 Aug 2018 07:08:59 -0500 Subject: [PATCH 05/12] utilize .is_unsafe_ptr --- clippy_lints/src/ptr_offset_with_cast.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/clippy_lints/src/ptr_offset_with_cast.rs b/clippy_lints/src/ptr_offset_with_cast.rs index af9989a161c..75451565510 100644 --- a/clippy_lints/src/ptr_offset_with_cast.rs +++ b/clippy_lints/src/ptr_offset_with_cast.rs @@ -1,4 +1,4 @@ -use rustc::{declare_lint, hir, lint, lint_array, ty}; +use rustc::{declare_lint, hir, lint, lint_array}; use crate::utils; /// **What it does:** Checks for usage of the `offset` pointer method with a `usize` casted to an @@ -107,11 +107,7 @@ fn is_expr_ty_raw_ptr<'a, 'tcx>( cx: &lint::LateContext<'a, 'tcx>, expr: &hir::Expr, ) -> bool { - if let ty::RawPtr(..) = cx.tables.expr_ty(expr).sty { - true - } else { - false - } + cx.tables.expr_ty(expr).is_unsafe_ptr() } fn build_suggestion<'a, 'tcx>( From 61c20c148e926310f011895693f88cafb5a26539 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Wed, 29 Aug 2018 07:12:22 -0500 Subject: [PATCH 06/12] if no suggestion, dont add suggestion --- clippy_lints/src/ptr_offset_with_cast.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/clippy_lints/src/ptr_offset_with_cast.rs b/clippy_lints/src/ptr_offset_with_cast.rs index 75451565510..5fb31c4aefb 100644 --- a/clippy_lints/src/ptr_offset_with_cast.rs +++ b/clippy_lints/src/ptr_offset_with_cast.rs @@ -56,14 +56,13 @@ impl<'a, 'tcx> lint::LateLintPass<'a, 'tcx> for Pass { None => return, }; - utils::span_lint_and_sugg( - cx, - PTR_OFFSET_WITH_CAST, - expr.span, - "use of `offset` with a `usize` casted to an `isize`", - "try", - build_suggestion(cx, receiver_expr, cast_lhs_expr), - ); + let msg = "use of `offset` with a `usize` casted to an `isize`"; + if let Some(sugg) = build_suggestion(cx, receiver_expr, cast_lhs_expr) { + utils::span_lint_and_sugg(cx, PTR_OFFSET_WITH_CAST, expr.span, msg, "try", sugg); + } else { + utils::span_lint(cx, PTR_OFFSET_WITH_CAST, expr.span, msg); + } + } } @@ -114,12 +113,12 @@ fn build_suggestion<'a, 'tcx>( cx: &lint::LateContext<'a, 'tcx>, receiver_expr: &hir::Expr, cast_lhs_expr: &hir::Expr, -) -> String { +) -> Option { match ( utils::snippet_opt(cx, receiver_expr.span), utils::snippet_opt(cx, cast_lhs_expr.span) ) { - (Some(receiver), Some(cast_lhs)) => format!("{}.add({})", receiver, cast_lhs), - _ => String::new(), + (Some(receiver), Some(cast_lhs)) => Some(format!("{}.add({})", receiver, cast_lhs)), + _ => None, } } From 2fa7351c1e0ca482ae13ca969be5975f1603a2b1 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Wed, 29 Aug 2018 07:40:00 -0500 Subject: [PATCH 07/12] suggest wrapping_offset as well --- clippy_lints/src/ptr_offset_with_cast.rs | 58 ++++++++++++++++++------ tests/ui/ptr_offset_with_cast.rs | 4 ++ tests/ui/ptr_offset_with_cast.stderr | 8 +++- 3 files changed, 56 insertions(+), 14 deletions(-) diff --git a/clippy_lints/src/ptr_offset_with_cast.rs b/clippy_lints/src/ptr_offset_with_cast.rs index 5fb31c4aefb..20934573f09 100644 --- a/clippy_lints/src/ptr_offset_with_cast.rs +++ b/clippy_lints/src/ptr_offset_with_cast.rs @@ -1,5 +1,6 @@ use rustc::{declare_lint, hir, lint, lint_array}; use crate::utils; +use std::fmt; /// **What it does:** Checks for usage of the `offset` pointer method with a `usize` casted to an /// `isize`. @@ -44,23 +45,23 @@ impl lint::LintPass for Pass { impl<'a, 'tcx> lint::LateLintPass<'a, 'tcx> for Pass { fn check_expr(&mut self, cx: &lint::LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) { - // Check if the expressions is a ptr.offset method call - let [receiver_expr, arg_expr] = match expr_as_ptr_offset_call(cx, expr) { + // 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) { Some(call_arg) => call_arg, None => return, }; - // Check if the argument to ptr.offset is a cast from usize + // Check if the argument to the method call is a cast from usize let cast_lhs_expr = match expr_as_cast_from_usize(cx, arg_expr) { Some(cast_lhs_expr) => cast_lhs_expr, None => return, }; - let msg = "use of `offset` with a `usize` casted to an `isize`"; - if let Some(sugg) = build_suggestion(cx, receiver_expr, cast_lhs_expr) { - utils::span_lint_and_sugg(cx, PTR_OFFSET_WITH_CAST, expr.span, msg, "try", sugg); + 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) { + utils::span_lint_and_sugg(cx, PTR_OFFSET_WITH_CAST, expr.span, &msg, "try", sugg); } else { - utils::span_lint(cx, PTR_OFFSET_WITH_CAST, expr.span, msg); + utils::span_lint(cx, PTR_OFFSET_WITH_CAST, expr.span, &msg); } } @@ -79,15 +80,20 @@ fn expr_as_cast_from_usize<'a, 'tcx>( None } -// If the given expression is a ptr::offset method call, return the receiver and the arg of the -// method call. +// 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. fn expr_as_ptr_offset_call<'a, 'tcx>( cx: &lint::LateContext<'a, 'tcx>, expr: &'tcx hir::Expr, -) -> Option<[&'tcx hir::Expr; 2]> { +) -> Option<(&'tcx hir::Expr, &'tcx hir::Expr, Method)> { if let hir::ExprKind::MethodCall(ref path_segment, _, ref args) = expr.node { - if path_segment.ident.name == "offset" && is_expr_ty_raw_ptr(cx, &args[0]) { - return Some([&args[0], &args[1]]); + 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)); + } } } None @@ -111,6 +117,7 @@ fn is_expr_ty_raw_ptr<'a, 'tcx>( fn build_suggestion<'a, 'tcx>( cx: &lint::LateContext<'a, 'tcx>, + method: Method, receiver_expr: &hir::Expr, cast_lhs_expr: &hir::Expr, ) -> Option { @@ -118,7 +125,32 @@ fn build_suggestion<'a, 'tcx>( utils::snippet_opt(cx, receiver_expr.span), utils::snippet_opt(cx, cast_lhs_expr.span) ) { - (Some(receiver), Some(cast_lhs)) => Some(format!("{}.add({})", receiver, cast_lhs)), + (Some(receiver), Some(cast_lhs)) => { + Some(format!("{}.{}({})", receiver, method.suggestion(), cast_lhs)) + }, _ => None, } } + +enum Method { + Offset, + WrappingOffset, +} + +impl Method { + fn suggestion(&self) -> &'static str { + match *self { + Method::Offset => "add", + Method::WrappingOffset => "wrapping_add", + } + } +} + +impl fmt::Display for Method { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match *self { + Method::Offset => write!(f, "offset"), + Method::WrappingOffset => write!(f, "wrapping_offset"), + } + } +} \ No newline at end of file diff --git a/tests/ui/ptr_offset_with_cast.rs b/tests/ui/ptr_offset_with_cast.rs index 947021aaf8e..4549f960ca0 100644 --- a/tests/ui/ptr_offset_with_cast.rs +++ b/tests/ui/ptr_offset_with_cast.rs @@ -10,5 +10,9 @@ fn main() { ptr.offset(offset_usize as isize); ptr.offset(offset_isize as isize); ptr.offset(offset_u8 as isize); + + ptr.wrapping_offset(offset_usize as isize); + ptr.wrapping_offset(offset_isize as isize); + ptr.wrapping_offset(offset_u8 as isize); } } diff --git a/tests/ui/ptr_offset_with_cast.stderr b/tests/ui/ptr_offset_with_cast.stderr index 1658f5f5366..16bbf328b33 100644 --- a/tests/ui/ptr_offset_with_cast.stderr +++ b/tests/ui/ptr_offset_with_cast.stderr @@ -6,5 +6,11 @@ error: use of `offset` with a `usize` casted to an `isize` | = note: `-D ptr-offset-with-cast` implied by `-D warnings` -error: aborting due to previous error +error: use of `wrapping_offset` with a `usize` casted to an `isize` + --> $DIR/ptr_offset_with_cast.rs:14:9 + | +14 | ptr.wrapping_offset(offset_usize as isize); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr.wrapping_add(offset_usize)` + +error: aborting due to 2 previous errors From 2a486528ee13c861679c62a5451f7118591c3531 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Wed, 29 Aug 2018 07:42:43 -0500 Subject: [PATCH 08/12] utilize carrier --- clippy_lints/src/ptr_offset_with_cast.rs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/clippy_lints/src/ptr_offset_with_cast.rs b/clippy_lints/src/ptr_offset_with_cast.rs index 20934573f09..08ae92d255e 100644 --- a/clippy_lints/src/ptr_offset_with_cast.rs +++ b/clippy_lints/src/ptr_offset_with_cast.rs @@ -121,15 +121,9 @@ fn build_suggestion<'a, 'tcx>( receiver_expr: &hir::Expr, cast_lhs_expr: &hir::Expr, ) -> Option { - match ( - utils::snippet_opt(cx, receiver_expr.span), - utils::snippet_opt(cx, cast_lhs_expr.span) - ) { - (Some(receiver), Some(cast_lhs)) => { - Some(format!("{}.{}({})", receiver, method.suggestion(), cast_lhs)) - }, - _ => None, - } + 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)) } enum Method { From 9a8f206662d63d0218e7f8318c8ddbfccca6bfa8 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Wed, 29 Aug 2018 07:43:40 -0500 Subject: [PATCH 09/12] eof newline --- clippy_lints/src/ptr_offset_with_cast.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clippy_lints/src/ptr_offset_with_cast.rs b/clippy_lints/src/ptr_offset_with_cast.rs index 08ae92d255e..f8e35f84db1 100644 --- a/clippy_lints/src/ptr_offset_with_cast.rs +++ b/clippy_lints/src/ptr_offset_with_cast.rs @@ -147,4 +147,4 @@ impl fmt::Display for Method { Method::WrappingOffset => write!(f, "wrapping_offset"), } } -} \ No newline at end of file +} From 6445a5d79d642d38ea2efba3e8c08cf87ba40920 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Wed, 29 Aug 2018 08:01:05 -0500 Subject: [PATCH 10/12] derive copy/clone --- clippy_lints/src/ptr_offset_with_cast.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/clippy_lints/src/ptr_offset_with_cast.rs b/clippy_lints/src/ptr_offset_with_cast.rs index f8e35f84db1..7f34aca27f0 100644 --- a/clippy_lints/src/ptr_offset_with_cast.rs +++ b/clippy_lints/src/ptr_offset_with_cast.rs @@ -126,6 +126,7 @@ fn build_suggestion<'a, 'tcx>( Some(format!("{}.{}({})", receiver, method.suggestion(), cast_lhs)) } +#[derive(Copy, Clone)] enum Method { Offset, WrappingOffset, From 53928d53673303a2147eccb34918234886ac0e26 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Wed, 29 Aug 2018 08:27:32 -0500 Subject: [PATCH 11/12] clippy suggestion --- clippy_lints/src/ptr_offset_with_cast.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clippy_lints/src/ptr_offset_with_cast.rs b/clippy_lints/src/ptr_offset_with_cast.rs index 7f34aca27f0..6f589a7d1d7 100644 --- a/clippy_lints/src/ptr_offset_with_cast.rs +++ b/clippy_lints/src/ptr_offset_with_cast.rs @@ -133,7 +133,7 @@ enum Method { } impl Method { - fn suggestion(&self) -> &'static str { + fn suggestion(self) -> &'static str { match *self { Method::Offset => "add", Method::WrappingOffset => "wrapping_add", From f42442b6e07a8eafc711d708bb70d6065b4f8c5d Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Wed, 29 Aug 2018 08:59:38 -0500 Subject: [PATCH 12/12] dont deref --- clippy_lints/src/ptr_offset_with_cast.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/ptr_offset_with_cast.rs b/clippy_lints/src/ptr_offset_with_cast.rs index 6f589a7d1d7..9f475473c61 100644 --- a/clippy_lints/src/ptr_offset_with_cast.rs +++ b/clippy_lints/src/ptr_offset_with_cast.rs @@ -134,7 +134,7 @@ enum Method { impl Method { fn suggestion(self) -> &'static str { - match *self { + match self { Method::Offset => "add", Method::WrappingOffset => "wrapping_add", } @@ -143,7 +143,7 @@ impl Method { impl fmt::Display for Method { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match *self { + match self { Method::Offset => write!(f, "offset"), Method::WrappingOffset => write!(f, "wrapping_offset"), }