1 use rustc_ast::ast::{Expr, ExprKind};
2 use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
3 use rustc_middle::lint::in_external_macro;
4 use rustc_session::{declare_lint_pass, declare_tool_lint};
6 use crate::utils::span_lint_and_help;
9 /// **What it does:** Checks for usage of `as` conversions.
11 /// **Why is this bad?** `as` conversions will perform many kinds of
12 /// conversions, including silently lossy conversions and dangerous coercions.
13 /// There are cases when it makes sense to use `as`, so the lint is
16 /// **Known problems:** None.
25 /// Usually better represents the semantics you expect:
31 /// f(a.try_into().expect("Unexpected u16 overflow in f"));
36 "using a potentially dangerous silent `as` conversion"
39 declare_lint_pass!(AsConversions => [AS_CONVERSIONS]);
41 impl EarlyLintPass for AsConversions {
42 fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
43 if in_external_macro(cx.sess(), expr.span) {
47 if let ExprKind::Cast(_, _) = expr.kind {
52 "using a potentially dangerous silent `as` conversion",
54 "consider using a safe wrapper for this conversion",