mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-21 17:52:12 +03:00
other uses of get_diagnostic_name
This commit is contained in:
@@ -304,7 +304,9 @@ struct InsertExpr<'tcx> {
|
||||
fn try_parse_insert<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<InsertExpr<'tcx>> {
|
||||
if let ExprKind::MethodCall(_, map, [key, value], _) = expr.kind {
|
||||
let id = cx.typeck_results().type_dependent_def_id(expr.hir_id)?;
|
||||
if cx.tcx.is_diagnostic_item(sym::btreemap_insert, id) || cx.tcx.is_diagnostic_item(sym::hashmap_insert, id) {
|
||||
if let Some(insert) = cx.tcx.get_diagnostic_name(id)
|
||||
&& matches!(insert, sym::btreemap_insert | sym::hashmap_insert)
|
||||
{
|
||||
Some(InsertExpr { map, key, value })
|
||||
} else {
|
||||
None
|
||||
|
||||
@@ -215,7 +215,8 @@ fn check_replace_with_uninit(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<'
|
||||
&& let ExprKind::Path(ref repl_func_qpath) = repl_func.kind
|
||||
&& let Some(repl_def_id) = cx.qpath_res(repl_func_qpath, repl_func.hir_id).opt_def_id()
|
||||
{
|
||||
if cx.tcx.is_diagnostic_item(sym::mem_uninitialized, repl_def_id) {
|
||||
let repl_name = cx.tcx.get_diagnostic_name(repl_def_id);
|
||||
if repl_name == Some(sym::mem_uninitialized) {
|
||||
let Some(top_crate) = std_or_core(cx) else { return };
|
||||
let mut applicability = Applicability::MachineApplicable;
|
||||
span_lint_and_sugg(
|
||||
@@ -230,9 +231,7 @@ fn check_replace_with_uninit(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<'
|
||||
),
|
||||
applicability,
|
||||
);
|
||||
} else if cx.tcx.is_diagnostic_item(sym::mem_zeroed, repl_def_id)
|
||||
&& !cx.typeck_results().expr_ty(src).is_primitive()
|
||||
{
|
||||
} else if repl_name == Some(sym::mem_zeroed) && !cx.typeck_results().expr_ty(src).is_primitive() {
|
||||
span_lint_and_help(
|
||||
cx,
|
||||
MEM_REPLACE_WITH_UNINIT,
|
||||
|
||||
@@ -233,18 +233,16 @@ fn hir(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, filter_param_id: HirId) -
|
||||
// the latter only calls `effect` once
|
||||
let side_effect_expr_span = receiver.can_have_side_effects().then_some(receiver.span);
|
||||
|
||||
if cx.tcx.is_diagnostic_item(sym::Option, recv_ty.did()) && path.ident.name == sym::is_some {
|
||||
Some(Self::IsSome {
|
||||
match (cx.tcx.get_diagnostic_name(recv_ty.did()), path.ident.name) {
|
||||
(Some(sym::Option), sym::is_some) => Some(Self::IsSome {
|
||||
receiver,
|
||||
side_effect_expr_span,
|
||||
})
|
||||
} else if cx.tcx.is_diagnostic_item(sym::Result, recv_ty.did()) && path.ident.name == sym::is_ok {
|
||||
Some(Self::IsOk {
|
||||
}),
|
||||
(Some(sym::Result), sym::is_ok) => Some(Self::IsOk {
|
||||
receiver,
|
||||
side_effect_expr_span,
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}),
|
||||
_ => None,
|
||||
}
|
||||
} else if matching_root_macro_call(cx, expr.span, sym::matches_macro).is_some()
|
||||
// we know for a fact that the wildcard pattern is the second arm
|
||||
|
||||
@@ -50,10 +50,10 @@ fn try_get_caller_ty_name_and_method_name(
|
||||
}
|
||||
} else {
|
||||
if let ty::Adt(adt, _) = cx.typeck_results().expr_ty(caller_expr).kind() {
|
||||
if cx.tcx.is_diagnostic_item(sym::Option, adt.did()) {
|
||||
return Some(("Option", "and_then"));
|
||||
} else if cx.tcx.is_diagnostic_item(sym::Result, adt.did()) {
|
||||
return Some(("Result", "and_then"));
|
||||
match cx.tcx.get_diagnostic_name(adt.did()) {
|
||||
Some(sym::Option) => return Some(("Option", "and_then")),
|
||||
Some(sym::Result) => return Some(("Result", "and_then")),
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
None
|
||||
|
||||
@@ -5,10 +5,10 @@
|
||||
|
||||
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, receiver: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
|
||||
if let Some(fn_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) {
|
||||
if cx.tcx.is_diagnostic_item(sym::string_push_str, fn_def_id) {
|
||||
single_char_push_string::check(cx, expr, receiver, args);
|
||||
} else if cx.tcx.is_diagnostic_item(sym::string_insert_str, fn_def_id) {
|
||||
single_char_insert_string::check(cx, expr, receiver, args);
|
||||
match cx.tcx.get_diagnostic_name(fn_def_id) {
|
||||
Some(sym::string_push_str) => single_char_push_string::check(cx, expr, receiver, args),
|
||||
Some(sym::string_insert_str) => single_char_insert_string::check(cx, expr, receiver, args),
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +37,8 @@ pub(crate) fn check<'tcx>(
|
||||
// right hand side matches either f32::EPSILON or f64::EPSILON
|
||||
&& let ExprKind::Path(ref epsilon_path) = rhs.kind
|
||||
&& let Res::Def(DefKind::AssocConst, def_id) = cx.qpath_res(epsilon_path, rhs.hir_id)
|
||||
&& ([sym::f32_epsilon, sym::f64_epsilon].into_iter().any(|sym| cx.tcx.is_diagnostic_item(sym, def_id)))
|
||||
&& let Some(epsilon) = cx.tcx.get_diagnostic_name(def_id)
|
||||
&& matches!(epsilon, sym::f32_epsilon| sym::f64_epsilon)
|
||||
|
||||
// values of the subtractions on the left hand side are of the type float
|
||||
&& let t_val_l = cx.typeck_results().expr_ty(val_l)
|
||||
|
||||
@@ -89,7 +89,9 @@ fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx hir::Block<'tcx>)
|
||||
{
|
||||
// We don't want to lint inside io::Read or io::Write implementations, as the author has more
|
||||
// information about their trait implementation than our lint, see https://github.com/rust-lang/rust-clippy/issues/4836
|
||||
if cx.tcx.is_diagnostic_item(sym::IoRead, trait_id) || cx.tcx.is_diagnostic_item(sym::IoWrite, trait_id) {
|
||||
if let Some(trait_name) = cx.tcx.get_diagnostic_name(trait_id)
|
||||
&& matches!(trait_name, sym::IoRead | sym::IoWrite)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -144,11 +144,9 @@ fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
|
||||
|
||||
/// Checks if the given expression is a macro call to `todo!()` or `unimplemented!()`.
|
||||
pub fn is_todo_unimplemented_macro(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
|
||||
root_macro_call_first_node(cx, expr).is_some_and(|macro_call| {
|
||||
[sym::todo_macro, sym::unimplemented_macro]
|
||||
.iter()
|
||||
.any(|&sym| cx.tcx.is_diagnostic_item(sym, macro_call.def_id))
|
||||
})
|
||||
root_macro_call_first_node(cx, expr)
|
||||
.and_then(|macro_call| cx.tcx.get_diagnostic_name(macro_call.def_id))
|
||||
.is_some_and(|macro_name| matches!(macro_name, sym::todo_macro | sym::unimplemented_macro))
|
||||
}
|
||||
|
||||
/// Checks if the given expression is a stub, i.e., a `todo!()` or `unimplemented!()` expression,
|
||||
|
||||
Reference in New Issue
Block a user