if no suggestion, dont add suggestion

This commit is contained in:
Corey Farwell 2018-08-29 07:12:22 -05:00
parent f7d1ef90a0
commit 61c20c148e

View file

@ -56,14 +56,13 @@ impl<'a, 'tcx> lint::LateLintPass<'a, 'tcx> for Pass {
None => return, None => return,
}; };
utils::span_lint_and_sugg( let msg = "use of `offset` with a `usize` casted to an `isize`";
cx, if let Some(sugg) = build_suggestion(cx, receiver_expr, cast_lhs_expr) {
PTR_OFFSET_WITH_CAST, utils::span_lint_and_sugg(cx, PTR_OFFSET_WITH_CAST, expr.span, msg, "try", sugg);
expr.span, } else {
"use of `offset` with a `usize` casted to an `isize`", utils::span_lint(cx, PTR_OFFSET_WITH_CAST, expr.span, msg);
"try", }
build_suggestion(cx, receiver_expr, cast_lhs_expr),
);
} }
} }
@ -114,12 +113,12 @@ fn build_suggestion<'a, 'tcx>(
cx: &lint::LateContext<'a, 'tcx>, cx: &lint::LateContext<'a, 'tcx>,
receiver_expr: &hir::Expr, receiver_expr: &hir::Expr,
cast_lhs_expr: &hir::Expr, cast_lhs_expr: &hir::Expr,
) -> String { ) -> Option<String> {
match ( match (
utils::snippet_opt(cx, receiver_expr.span), utils::snippet_opt(cx, receiver_expr.span),
utils::snippet_opt(cx, cast_lhs_expr.span) utils::snippet_opt(cx, cast_lhs_expr.span)
) { ) {
(Some(receiver), Some(cast_lhs)) => format!("{}.add({})", receiver, cast_lhs), (Some(receiver), Some(cast_lhs)) => Some(format!("{}.add({})", receiver, cast_lhs)),
_ => String::new(), _ => None,
} }
} }