|
1 |
| -use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then, span_lint_hir_and_then}; |
2 |
| -use clippy_utils::source::{snippet, snippet_opt, snippet_with_context}; |
| 1 | +use clippy_utils::diagnostics::{span_lint, span_lint_and_then, span_lint_hir_and_then}; |
| 2 | +use clippy_utils::source::{snippet, snippet_with_context}; |
3 | 3 | use clippy_utils::sugg::Sugg;
|
4 | 4 | use clippy_utils::{
|
5 |
| - any_parent_is_automatically_derived, fulfill_or_allowed, get_parent_expr, in_constant, is_integer_literal, |
6 |
| - is_lint_allowed, is_no_std_crate, iter_input_pats, last_path_segment, SpanlessEq, |
| 5 | + any_parent_is_automatically_derived, fulfill_or_allowed, get_parent_expr, is_lint_allowed, iter_input_pats, |
| 6 | + last_path_segment, SpanlessEq, |
7 | 7 | };
|
8 | 8 | use if_chain::if_chain;
|
9 | 9 | use rustc_errors::Applicability;
|
10 | 10 | use rustc_hir::def::Res;
|
11 | 11 | use rustc_hir::intravisit::FnKind;
|
12 | 12 | use rustc_hir::{
|
13 |
| - BinOpKind, BindingAnnotation, Body, ByRef, Expr, ExprKind, FnDecl, Mutability, PatKind, QPath, Stmt, StmtKind, Ty, |
14 |
| - TyKind, |
| 13 | + BinOpKind, BindingAnnotation, Body, ByRef, Expr, ExprKind, FnDecl, Mutability, PatKind, QPath, Stmt, StmtKind, |
15 | 14 | };
|
16 | 15 | use rustc_lint::{LateContext, LateLintPass, LintContext};
|
17 | 16 | use rustc_middle::lint::in_external_macro;
|
18 |
| -use rustc_session::{declare_tool_lint, impl_lint_pass}; |
| 17 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
19 | 18 | use rustc_span::def_id::LocalDefId;
|
20 | 19 | use rustc_span::source_map::Span;
|
21 | 20 |
|
@@ -55,6 +54,7 @@ declare_clippy_lint! {
|
55 | 54 | style,
|
56 | 55 | "an entire binding declared as `ref`, in a function argument or a `let` statement"
|
57 | 56 | }
|
| 57 | + |
58 | 58 | declare_clippy_lint! {
|
59 | 59 | /// ### What it does
|
60 | 60 | /// Checks for the use of bindings with a single leading
|
@@ -102,51 +102,13 @@ declare_clippy_lint! {
|
102 | 102 | "using a short circuit boolean condition as a statement"
|
103 | 103 | }
|
104 | 104 |
|
105 |
| -declare_clippy_lint! { |
106 |
| - /// ### What it does |
107 |
| - /// Catch casts from `0` to some pointer type |
108 |
| - /// |
109 |
| - /// ### Why is this bad? |
110 |
| - /// This generally means `null` and is better expressed as |
111 |
| - /// {`std`, `core`}`::ptr::`{`null`, `null_mut`}. |
112 |
| - /// |
113 |
| - /// ### Example |
114 |
| - /// ```rust |
115 |
| - /// let a = 0 as *const u32; |
116 |
| - /// ``` |
117 |
| - /// |
118 |
| - /// Use instead: |
119 |
| - /// ```rust |
120 |
| - /// let a = std::ptr::null::<u32>(); |
121 |
| - /// ``` |
122 |
| - #[clippy::version = "pre 1.29.0"] |
123 |
| - pub ZERO_PTR, |
124 |
| - style, |
125 |
| - "using `0 as *{const, mut} T`" |
126 |
| -} |
127 |
| - |
128 |
| -pub struct LintPass { |
129 |
| - std_or_core: &'static str, |
130 |
| -} |
131 |
| -impl Default for LintPass { |
132 |
| - fn default() -> Self { |
133 |
| - Self { std_or_core: "std" } |
134 |
| - } |
135 |
| -} |
136 |
| -impl_lint_pass!(LintPass => [ |
| 105 | +declare_lint_pass!(LintPass => [ |
137 | 106 | TOPLEVEL_REF_ARG,
|
138 | 107 | USED_UNDERSCORE_BINDING,
|
139 | 108 | SHORT_CIRCUIT_STATEMENT,
|
140 |
| - ZERO_PTR, |
141 | 109 | ]);
|
142 | 110 |
|
143 | 111 | impl<'tcx> LateLintPass<'tcx> for LintPass {
|
144 |
| - fn check_crate(&mut self, cx: &LateContext<'_>) { |
145 |
| - if is_no_std_crate(cx) { |
146 |
| - self.std_or_core = "core"; |
147 |
| - } |
148 |
| - } |
149 |
| - |
150 | 112 | fn check_fn(
|
151 | 113 | &mut self,
|
152 | 114 | cx: &LateContext<'tcx>,
|
@@ -252,10 +214,6 @@ impl<'tcx> LateLintPass<'tcx> for LintPass {
|
252 | 214 | }
|
253 | 215 |
|
254 | 216 | fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
255 |
| - if let ExprKind::Cast(e, ty) = expr.kind { |
256 |
| - self.check_cast(cx, expr.span, e, ty); |
257 |
| - return; |
258 |
| - } |
259 | 217 | if in_external_macro(cx.sess(), expr.span)
|
260 | 218 | || expr.span.desugaring_kind().is_some()
|
261 | 219 | || any_parent_is_automatically_derived(cx.tcx, expr.hir_id)
|
@@ -320,29 +278,3 @@ fn is_used(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
|
320 | 278 | _ => is_used(cx, parent),
|
321 | 279 | })
|
322 | 280 | }
|
323 |
| - |
324 |
| -impl LintPass { |
325 |
| - fn check_cast(&self, cx: &LateContext<'_>, span: Span, e: &Expr<'_>, ty: &Ty<'_>) { |
326 |
| - if_chain! { |
327 |
| - if let TyKind::Ptr(ref mut_ty) = ty.kind; |
328 |
| - if is_integer_literal(e, 0); |
329 |
| - if !in_constant(cx, e.hir_id); |
330 |
| - then { |
331 |
| - let (msg, sugg_fn) = match mut_ty.mutbl { |
332 |
| - Mutability::Mut => ("`0 as *mut _` detected", "ptr::null_mut"), |
333 |
| - Mutability::Not => ("`0 as *const _` detected", "ptr::null"), |
334 |
| - }; |
335 |
| - |
336 |
| - let (sugg, appl) = if let TyKind::Infer = mut_ty.ty.kind { |
337 |
| - (format!("{}::{sugg_fn}()", self.std_or_core), Applicability::MachineApplicable) |
338 |
| - } else if let Some(mut_ty_snip) = snippet_opt(cx, mut_ty.ty.span) { |
339 |
| - (format!("{}::{sugg_fn}::<{mut_ty_snip}>()", self.std_or_core), Applicability::MachineApplicable) |
340 |
| - } else { |
341 |
| - // `MaybeIncorrect` as type inference may not work with the suggested code |
342 |
| - (format!("{}::{sugg_fn}()", self.std_or_core), Applicability::MaybeIncorrect) |
343 |
| - }; |
344 |
| - span_lint_and_sugg(cx, ZERO_PTR, span, msg, "try", sugg, appl); |
345 |
| - } |
346 |
| - } |
347 |
| - } |
348 |
| -} |
0 commit comments