Improve error messages
This commit is contained in:
parent
316e9db0cf
commit
49f32e0c8e
2 changed files with 25 additions and 21 deletions
|
@ -39,7 +39,7 @@ declare_lint_pass!(NoopMethodCall => [NOOP_METHOD_CALL]);
|
|||
impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
|
||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
// We only care about method calls
|
||||
if let ExprKind::MethodCall(call, ..) = expr.kind {
|
||||
if let ExprKind::MethodCall(call, _, elements, _) = expr.kind {
|
||||
// Get the `DefId` only when dealing with an `AssocFn`
|
||||
if let Some((DefKind::AssocFn, did)) =
|
||||
cx.typeck_results().type_dependent_def(expr.hir_id)
|
||||
|
@ -70,15 +70,23 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
|
|||
.iter()
|
||||
.any(|s| cx.tcx.is_diagnostic_item(*s, i.def_id()))
|
||||
{
|
||||
let method = &call.ident.name;
|
||||
let receiver = &elements[0];
|
||||
let receiver_ty = cx.typeck_results().expr_ty(receiver);
|
||||
let expr_span = expr.span;
|
||||
let note = format!(
|
||||
"the type `{:?}` which `{}` is being called on is the same as the type returned from `{}`, \
|
||||
so the method call does not do anything and can be removed.",
|
||||
receiver_ty, method, method
|
||||
);
|
||||
|
||||
cx.struct_span_lint(NOOP_METHOD_CALL, expr_span, |lint| {
|
||||
let span = expr_span.with_lo(receiver.span.hi());
|
||||
cx.struct_span_lint(NOOP_METHOD_CALL, span, |lint| {
|
||||
let method = &call.ident.name;
|
||||
let message = format!("call to `.{}()` on a reference in this situation does nothing", &method);
|
||||
lint.build(&message)
|
||||
.span_label(expr_span, "unnecessary method call")
|
||||
.note("the type the method is being called on and the return type are functionally equivalent.")
|
||||
.note("therefore, the method call doesn't actually do anything and can be removed.")
|
||||
.span_label(span, "unnecessary method call")
|
||||
.note(¬e)
|
||||
.emit()
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue