Skip to content

WIP Suggestion creation macro #7986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clippy_lints/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ edition = "2021"
[dependencies]
cargo_metadata = "0.14"
clippy_utils = { path = "../clippy_utils" }
clippy_macros = { path = "../clippy_macros" }
if_chain = "1.0"
itertools = "0.10.1"
pulldown-cmark = { version = "0.9", default-features = false }
Expand Down
21 changes: 7 additions & 14 deletions clippy_lints/src/from_str_radix_10.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::sugg::Sugg;
use clippy_macros::expr_sugg;
use clippy_utils::_internal::lint_expr_and_sugg;
use clippy_utils::ty::is_type_diagnostic_item;
use if_chain::if_chain;
use rustc_errors::Applicability;
Expand Down Expand Up @@ -44,7 +44,7 @@ declare_clippy_lint! {
declare_lint_pass!(FromStrRadix10 => [FROM_STR_RADIX_10]);

impl<'tcx> LateLintPass<'tcx> for FromStrRadix10 {
fn check_expr(&mut self, cx: &LateContext<'tcx>, exp: &Expr<'tcx>) {
fn check_expr(&mut self, cx: &LateContext<'tcx>, exp: &'tcx Expr<'_>) {
if_chain! {
if let ExprKind::Call(maybe_path, arguments) = &exp.kind;
if let ExprKind::Path(QPath::TypeRelative(ty, pathseg)) = &maybe_path.kind;
Expand Down Expand Up @@ -76,21 +76,14 @@ impl<'tcx> LateLintPass<'tcx> for FromStrRadix10 {
&arguments[0]
};

let sugg = Sugg::hir_with_applicability(
cx,
expr,
"<string>",
&mut Applicability::MachineApplicable
).maybe_par();

span_lint_and_sugg(
lint_expr_and_sugg(
cx,
FROM_STR_RADIX_10,
exp.span,
"this call to `from_str_radix` can be replaced with a call to `str::parse`",
exp,
"try",
format!("{}.parse::<{}>()", sugg, prim_ty.name_str()),
Applicability::MaybeIncorrect
expr_sugg!({}.parse::<{}>(), expr, prim_ty.name_str()),
Applicability::MaybeIncorrect,
);
}
}
Expand Down
19 changes: 7 additions & 12 deletions clippy_lints/src/methods/bytes_nth.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_applicability;
use clippy_macros::expr_sugg;
use clippy_utils::_internal::lint_expr_and_sugg;
use clippy_utils::ty::is_type_diagnostic_item;
use rustc_errors::Applicability;
use rustc_hir::Expr;
Expand All @@ -8,7 +8,7 @@ use rustc_span::sym;

use super::BYTES_NTH;

pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, recv: &'tcx Expr<'tcx>, n_arg: &'tcx Expr<'tcx>) {
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, recv: &'tcx Expr<'_>, n_arg: &'tcx Expr<'_>) {
let ty = cx.typeck_results().expr_ty(recv).peel_refs();
let caller_type = if ty.is_str() {
"str"
Expand All @@ -17,18 +17,13 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, recv: &'tcx E
} else {
return;
};
let mut applicability = Applicability::MachineApplicable;
span_lint_and_sugg(
lint_expr_and_sugg(
cx,
BYTES_NTH,
expr.span,
&format!("called `.bytes().nth()` on a `{}`", caller_type),
expr,
"try",
format!(
"{}.as_bytes().get({})",
snippet_with_applicability(cx, recv.span, "..", &mut applicability),
snippet_with_applicability(cx, n_arg.span, "..", &mut applicability)
),
applicability,
expr_sugg!({}.as_bytes().get({}), recv, n_arg),
Applicability::MachineApplicable,
);
}
18 changes: 7 additions & 11 deletions clippy_lints/src/methods/iter_count.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::utils::derefs_to_slice;
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_applicability;
use clippy_macros::expr_sugg;
use clippy_utils::_internal::lint_expr_and_sugg;
use clippy_utils::ty::is_type_diagnostic_item;
use rustc_errors::Applicability;
use rustc_hir::Expr;
Expand All @@ -9,7 +9,7 @@ use rustc_span::sym;

use super::ITER_COUNT;

pub(crate) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, recv: &'tcx Expr<'tcx>, iter_method: &str) {
pub(crate) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, recv: &'tcx Expr<'_>, iter_method: &str) {
let ty = cx.typeck_results().expr_ty(recv);
let caller_type = if derefs_to_slice(cx, recv, ty).is_some() {
"slice"
Expand All @@ -32,17 +32,13 @@ pub(crate) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, recv: &'tcx E
} else {
return;
};
let mut applicability = Applicability::MachineApplicable;
span_lint_and_sugg(
lint_expr_and_sugg(
cx,
ITER_COUNT,
expr.span,
&format!("called `.{}().count()` on a `{}`", iter_method, caller_type),
expr,
"try",
format!(
"{}.len()",
snippet_with_applicability(cx, recv.span, "..", &mut applicability),
),
applicability,
expr_sugg!({}.len(), recv),
Applicability::MachineApplicable,
);
}
8 changes: 4 additions & 4 deletions clippy_lints/src/methods/iter_nth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use super::ITER_NTH;

pub(super) fn check<'tcx>(
cx: &LateContext<'tcx>,
expr: &hir::Expr<'_>,
iter_recv: &'tcx hir::Expr<'tcx>,
nth_recv: &hir::Expr<'_>,
nth_arg: &hir::Expr<'_>,
expr: &'tcx hir::Expr<'_>,
iter_recv: &'tcx hir::Expr<'_>,
nth_recv: &'tcx hir::Expr<'_>,
nth_arg: &'tcx hir::Expr<'_>,
is_mut: bool,
) {
let mut_str = if is_mut { "_mut" } else { "" };
Expand Down
20 changes: 12 additions & 8 deletions clippy_lints/src/methods/iter_nth_zero.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clippy_macros::expr_sugg;
use clippy_utils::_internal::lint_expr_and_sugg;
use clippy_utils::consts::{constant, Constant};
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::is_trait_method;
use clippy_utils::source::snippet_with_applicability;
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir as hir;
Expand All @@ -10,20 +10,24 @@ use rustc_span::sym;

use super::ITER_NTH_ZERO;

pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, arg: &hir::Expr<'_>) {
pub(super) fn check<'tcx>(
cx: &LateContext<'tcx>,
expr: &'tcx hir::Expr<'_>,
recv: &'tcx hir::Expr<'_>,
arg: &'tcx hir::Expr<'_>,
) {
if_chain! {
if is_trait_method(cx, expr, sym::Iterator);
if let Some((Constant::Int(0), _)) = constant(cx, cx.typeck_results(), arg);
then {
let mut applicability = Applicability::MachineApplicable;
span_lint_and_sugg(
lint_expr_and_sugg(
cx,
ITER_NTH_ZERO,
expr.span,
"called `.nth(0)` on a `std::iter::Iterator`, when `.next()` is equivalent",
expr,
"try calling `.next()` instead of `.nth(0)`",
format!("{}.next()", snippet_with_applicability(cx, recv.span, "..", &mut applicability)),
applicability,
expr_sugg!({}.next(), recv),
Applicability::MachineApplicable,
);
}
}
Expand Down
19 changes: 6 additions & 13 deletions clippy_lints/src/methods/unwrap_or_else_default.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
//! Lint for `some_result_or_option.unwrap_or_else(Default::default)`

use super::UNWRAP_OR_ELSE_DEFAULT;
use clippy_utils::{
diagnostics::span_lint_and_sugg, is_default_equivalent_call, source::snippet_with_applicability,
ty::is_type_diagnostic_item,
};
use clippy_macros::expr_sugg;
use clippy_utils::{_internal::lint_expr_and_sugg, is_default_equivalent_call, ty::is_type_diagnostic_item};
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_lint::LateContext;
Expand All @@ -27,19 +25,14 @@ pub(super) fn check<'tcx>(
if is_option || is_result;
if is_default_equivalent_call(cx, u_arg);
then {
let mut applicability = Applicability::MachineApplicable;

span_lint_and_sugg(
lint_expr_and_sugg(
cx,
UNWRAP_OR_ELSE_DEFAULT,
expr.span,
"use of `.unwrap_or_else(..)` to construct default value",
expr,
"try",
format!(
"{}.unwrap_or_default()",
snippet_with_applicability(cx, recv.span, "..", &mut applicability)
),
applicability,
expr_sugg!({}.unwrap_or_default(), recv),
Applicability::MachineApplicable,
);
}
}
Expand Down
20 changes: 20 additions & 0 deletions clippy_macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "clippy_macros"
version = "0.1.0"
edition = "2021"
publish = false

[lib]
proc-macro = true

[dev-dependencies]
clippy_utils = { path = "../clippy_utils" }

[dependencies]
proc-macro2 = "1.0.32"
quote = "1.0.10"

[dependencies.syn]
version = "1"
features = ["parsing", "proc-macro"]
default-features = false
14 changes: 14 additions & 0 deletions clippy_macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#![feature(concat_idents)]
#![feature(exact_size_is_empty)]
#![feature(let_else)]

mod sugg;

extern crate proc_macro;
use proc_macro::TokenStream;
use syn::parse_macro_input;

#[proc_macro]
pub fn expr_sugg(input: TokenStream) -> TokenStream {
parse_macro_input!(input as sugg::ExprSugg).0.into()
}
Loading