Auto merge of #10911 - lochetti:fix_9657, r=Alexendoo

Don't linting `as_conversions` in proc macros

Don't linting `as_conversions` if code was generated by procedural macro.

This PR fixes https://github.com/rust-lang/rust-clippy/issues/9657

I implemented the fix changing the lint code to be a `LateLintPass` in order to be able to use the `is_from_proc_macro` out of the box. If the reviwer thinks that it would be better to do the other way (implementing `WithSearchPat`) just let me know. I might need some help in implementing it for the `ustc_ast::ast::Expr`

changelog: [`as_conversions`] avoiding warnings in macro-generated code
This commit is contained in:
bors 2023-06-12 12:28:36 +00:00
commit b095247ab6
4 changed files with 20 additions and 10 deletions

View File

@ -1,6 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_help; use clippy_utils::diagnostics::span_lint_and_help;
use rustc_ast::ast::{Expr, ExprKind}; use clippy_utils::is_from_proc_macro;
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro; use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
@ -45,9 +46,9 @@ declare_clippy_lint! {
declare_lint_pass!(AsConversions => [AS_CONVERSIONS]); declare_lint_pass!(AsConversions => [AS_CONVERSIONS]);
impl EarlyLintPass for AsConversions { impl<'tcx> LateLintPass<'tcx> for AsConversions {
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
if in_external_macro(cx.sess(), expr.span) { if in_external_macro(cx.sess(), expr.span) || is_from_proc_macro(cx, expr) {
return; return;
} }

View File

@ -850,7 +850,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(move |_| Box::new(large_stack_arrays::LargeStackArrays::new(array_size_threshold))); store.register_late_pass(move |_| Box::new(large_stack_arrays::LargeStackArrays::new(array_size_threshold)));
store.register_late_pass(move |_| Box::new(large_const_arrays::LargeConstArrays::new(array_size_threshold))); store.register_late_pass(move |_| Box::new(large_const_arrays::LargeConstArrays::new(array_size_threshold)));
store.register_late_pass(|_| Box::new(floating_point_arithmetic::FloatingPointArithmetic)); store.register_late_pass(|_| Box::new(floating_point_arithmetic::FloatingPointArithmetic));
store.register_early_pass(|| Box::new(as_conversions::AsConversions)); store.register_late_pass(|_| Box::new(as_conversions::AsConversions));
store.register_late_pass(|_| Box::new(let_underscore::LetUnderscore)); store.register_late_pass(|_| Box::new(let_underscore::LetUnderscore));
store.register_early_pass(|| Box::<single_component_path_imports::SingleComponentPathImports>::default()); store.register_early_pass(|| Box::<single_component_path_imports::SingleComponentPathImports>::default());
let max_fn_params_bools = conf.max_fn_params_bools; let max_fn_params_bools = conf.max_fn_params_bools;

View File

@ -1,10 +1,11 @@
//@aux-build:proc_macros.rs //@aux-build:proc_macros.rs
#![warn(clippy::as_conversions)] #![warn(clippy::as_conversions)]
#![allow(clippy::borrow_as_ptr)] #![allow(clippy::borrow_as_ptr, unused)]
extern crate proc_macros; extern crate proc_macros;
use proc_macros::external; use proc_macros::external;
use proc_macros::with_span;
fn main() { fn main() {
let i = 0u32 as u64; let i = 0u32 as u64;
@ -13,3 +14,11 @@ fn main() {
external!(0u32 as u64); external!(0u32 as u64);
} }
with_span!(
span
fn coverting() {
let x = 0u32 as u64;
}
);

View File

@ -1,5 +1,5 @@
error: using a potentially dangerous silent `as` conversion error: using a potentially dangerous silent `as` conversion
--> $DIR/as_conversions.rs:10:13 --> $DIR/as_conversions.rs:11:13
| |
LL | let i = 0u32 as u64; LL | let i = 0u32 as u64;
| ^^^^^^^^^^^ | ^^^^^^^^^^^
@ -8,7 +8,7 @@ LL | let i = 0u32 as u64;
= note: `-D clippy::as-conversions` implied by `-D warnings` = note: `-D clippy::as-conversions` implied by `-D warnings`
error: using a potentially dangerous silent `as` conversion error: using a potentially dangerous silent `as` conversion
--> $DIR/as_conversions.rs:12:13 --> $DIR/as_conversions.rs:13:13
| |
LL | let j = &i as *const u64 as *mut u64; LL | let j = &i as *const u64 as *mut u64;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -16,7 +16,7 @@ LL | let j = &i as *const u64 as *mut u64;
= help: consider using a safe wrapper for this conversion = help: consider using a safe wrapper for this conversion
error: using a potentially dangerous silent `as` conversion error: using a potentially dangerous silent `as` conversion
--> $DIR/as_conversions.rs:12:13 --> $DIR/as_conversions.rs:13:13
| |
LL | let j = &i as *const u64 as *mut u64; LL | let j = &i as *const u64 as *mut u64;
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^