Skip to content

Commit dc78a89

Browse files
fee1-deadpietroalbini
authored andcommitted
do not use stringly typed diagnostics
1 parent e274006 commit dc78a89

File tree

3 files changed

+31
-26
lines changed

3 files changed

+31
-26
lines changed

compiler/rustc_lint/messages.ftl

+5-7
Original file line numberDiff line numberDiff line change
@@ -463,13 +463,11 @@ lint_requested_level = requested on the command line with `{$level} {$lint_name}
463463
lint_supertrait_as_deref_target = `{$t}` implements `Deref` with supertrait `{$target_principal}` as target
464464
.label = target type is set here
465465
466-
lint_suspicious_double_ref_op =
467-
using `.{$call}()` on a double reference, which returns `{$ty}` instead of {$op ->
468-
*[should_not_happen] [{$op}]
469-
[deref] dereferencing
470-
[borrow] borrowing
471-
[clone] cloning
472-
} the inner type
466+
lint_suspicious_double_ref_clone =
467+
using `.clone()` on a double reference, which returns `{$ty}` instead of cloning the inner type
468+
469+
lint_suspicious_double_ref_deref =
470+
using `.deref()` on a double reference, which returns `{$ty}` instead of dereferencing the inner type
473471
474472
lint_trivial_untranslatable_diag = diagnostic with static strings only
475473

compiler/rustc_lint/src/lints.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1188,11 +1188,15 @@ pub struct NoopMethodCallDiag<'a> {
11881188
}
11891189

11901190
#[derive(LintDiagnostic)]
1191-
#[diag(lint_suspicious_double_ref_op)]
1192-
pub struct SuspiciousDoubleRefDiag<'a> {
1193-
pub call: Symbol,
1191+
#[diag(lint_suspicious_double_ref_deref)]
1192+
pub struct SuspiciousDoubleRefDerefDiag<'a> {
1193+
pub ty: Ty<'a>,
1194+
}
1195+
1196+
#[derive(LintDiagnostic)]
1197+
#[diag(lint_suspicious_double_ref_clone)]
1198+
pub struct SuspiciousDoubleRefCloneDiag<'a> {
11941199
pub ty: Ty<'a>,
1195-
pub op: &'static str,
11961200
}
11971201

11981202
// pass_by_value.rs

compiler/rustc_lint/src/noop_method_call.rs

+18-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::context::LintContext;
2-
use crate::lints::{NoopMethodCallDiag, SuspiciousDoubleRefDiag};
2+
use crate::lints::{
3+
NoopMethodCallDiag, SuspiciousDoubleRefCloneDiag, SuspiciousDoubleRefDerefDiag,
4+
};
35
use crate::LateContext;
46
use crate::LateLintPass;
57
use rustc_hir::def::DefKind;
@@ -102,13 +104,6 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
102104
// (Re)check that it implements the noop diagnostic.
103105
let Some(name) = cx.tcx.get_diagnostic_name(i.def_id()) else { return };
104106

105-
let op = match name {
106-
sym::noop_method_borrow => "borrow",
107-
sym::noop_method_clone => "clone",
108-
sym::noop_method_deref => "deref",
109-
_ => return,
110-
};
111-
112107
let receiver_ty = cx.typeck_results().expr_ty(receiver);
113108
let expr_ty = cx.typeck_results().expr_ty_adjusted(expr);
114109
let arg_adjustments = cx.typeck_results().expr_adjustments(receiver);
@@ -129,14 +124,22 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
129124
NoopMethodCallDiag { method: call.ident.name, receiver_ty, label: span },
130125
);
131126
} else {
132-
if op == "borrow" {
133-
return;
127+
match name {
128+
// If `type_of(x) == T` and `x.borrow()` is used to get `&T`,
129+
// then that should be allowed
130+
sym::noop_method_borrow => return,
131+
sym::noop_method_clone => cx.emit_spanned_lint(
132+
SUSPICIOUS_DOUBLE_REF_OP,
133+
span,
134+
SuspiciousDoubleRefCloneDiag { ty: expr_ty },
135+
),
136+
sym::noop_method_deref => cx.emit_spanned_lint(
137+
SUSPICIOUS_DOUBLE_REF_OP,
138+
span,
139+
SuspiciousDoubleRefDerefDiag { ty: expr_ty },
140+
),
141+
_ => return,
134142
}
135-
cx.emit_spanned_lint(
136-
SUSPICIOUS_DOUBLE_REF_OP,
137-
span,
138-
SuspiciousDoubleRefDiag { call: call.ident.name, ty: expr_ty, op },
139-
)
140143
}
141144
}
142145
}

0 commit comments

Comments
 (0)