From 61c20c148e926310f011895693f88cafb5a26539 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Wed, 29 Aug 2018 07:12:22 -0500 Subject: [PATCH] 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, } }