2023-07-17 03:19:29 -05:00
|
|
|
use std::fmt;
|
|
|
|
use std::hash::{Hash, Hasher};
|
2021-07-01 11:17:38 -05:00
|
|
|
|
2022-10-06 02:44:38 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2021-12-06 05:33:31 -06:00
|
|
|
use clippy_utils::source::snippet_opt;
|
2021-07-01 11:17:38 -05:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_ast::ast;
|
|
|
|
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
2022-10-06 02:44:38 -05:00
|
|
|
use rustc_errors::Applicability;
|
2021-08-12 04:16:25 -05:00
|
|
|
use rustc_hir::def_id::DefId;
|
2021-07-01 11:17:38 -05:00
|
|
|
use rustc_lint::{EarlyContext, EarlyLintPass};
|
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2021-12-06 05:33:31 -06:00
|
|
|
use rustc_span::hygiene::{ExpnKind, MacroKind};
|
2022-10-06 02:44:38 -05:00
|
|
|
use rustc_span::Span;
|
2021-07-01 11:17:38 -05:00
|
|
|
use serde::{de, Deserialize};
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks that common macros are used with consistent bracing.
|
2021-07-01 11:17:38 -05:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// This is mostly a consistency lint although using () or []
|
2021-07-01 11:17:38 -05:00
|
|
|
/// doesn't give you a semicolon in item position, which can be unexpected.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2021-07-01 11:17:38 -05:00
|
|
|
/// ```rust
|
|
|
|
/// vec!{1, 2, 3};
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
|
|
|
/// vec![1, 2, 3];
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "1.55.0"]
|
2021-07-01 11:17:38 -05:00
|
|
|
pub NONSTANDARD_MACRO_BRACES,
|
2021-07-15 03:44:10 -05:00
|
|
|
nursery,
|
2021-07-01 11:17:38 -05:00
|
|
|
"check consistent use of braces in macro"
|
|
|
|
}
|
|
|
|
|
|
|
|
const BRACES: &[(&str, &str)] = &[("(", ")"), ("{", "}"), ("[", "]")];
|
|
|
|
|
2022-10-06 02:44:38 -05:00
|
|
|
/// The (callsite span, (open brace, close brace), source snippet)
|
|
|
|
type MacroInfo<'a> = (Span, &'a (String, String), String);
|
2021-07-01 11:17:38 -05:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
pub struct MacroBraces {
|
|
|
|
macro_braces: FxHashMap<String, (String, String)>,
|
|
|
|
done: FxHashSet<Span>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MacroBraces {
|
|
|
|
pub fn new(conf: &FxHashSet<MacroMatcher>) -> Self {
|
|
|
|
let macro_braces = macro_braces(conf.clone());
|
|
|
|
Self {
|
|
|
|
macro_braces,
|
|
|
|
done: FxHashSet::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(MacroBraces => [NONSTANDARD_MACRO_BRACES]);
|
|
|
|
|
|
|
|
impl EarlyLintPass for MacroBraces {
|
|
|
|
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
|
2022-10-06 02:44:38 -05:00
|
|
|
if let Some((span, braces, snip)) = is_offending_macro(cx, item.span, self) {
|
|
|
|
emit_help(cx, &snip, braces, span);
|
2021-07-01 11:17:38 -05:00
|
|
|
self.done.insert(span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_stmt(&mut self, cx: &EarlyContext<'_>, stmt: &ast::Stmt) {
|
2022-10-06 02:44:38 -05:00
|
|
|
if let Some((span, braces, snip)) = is_offending_macro(cx, stmt.span, self) {
|
|
|
|
emit_help(cx, &snip, braces, span);
|
2021-07-01 11:17:38 -05:00
|
|
|
self.done.insert(span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
|
2022-10-06 02:44:38 -05:00
|
|
|
if let Some((span, braces, snip)) = is_offending_macro(cx, expr.span, self) {
|
|
|
|
emit_help(cx, &snip, braces, span);
|
2021-07-01 11:17:38 -05:00
|
|
|
self.done.insert(span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_ty(&mut self, cx: &EarlyContext<'_>, ty: &ast::Ty) {
|
2022-10-06 02:44:38 -05:00
|
|
|
if let Some((span, braces, snip)) = is_offending_macro(cx, ty.span, self) {
|
|
|
|
emit_help(cx, &snip, braces, span);
|
2021-07-01 11:17:38 -05:00
|
|
|
self.done.insert(span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 03:44:10 -05:00
|
|
|
fn is_offending_macro<'a>(cx: &EarlyContext<'_>, span: Span, mac_braces: &'a MacroBraces) -> Option<MacroInfo<'a>> {
|
2021-08-12 04:16:25 -05:00
|
|
|
let unnested_or_local = || {
|
2021-12-06 05:33:31 -06:00
|
|
|
!span.ctxt().outer_expn_data().call_site.from_expansion()
|
2021-08-12 04:16:25 -05:00
|
|
|
|| span
|
|
|
|
.macro_backtrace()
|
|
|
|
.last()
|
|
|
|
.map_or(false, |e| e.macro_def_id.map_or(false, DefId::is_local))
|
|
|
|
};
|
2022-10-06 02:44:38 -05:00
|
|
|
let span_call_site = span.ctxt().outer_expn_data().call_site;
|
2021-07-01 11:17:38 -05:00
|
|
|
if_chain! {
|
2021-12-06 05:33:31 -06:00
|
|
|
if let ExpnKind::Macro(MacroKind::Bang, mac_name) = span.ctxt().outer_expn_data().kind;
|
2021-12-14 21:39:23 -06:00
|
|
|
let name = mac_name.as_str();
|
2021-12-06 05:33:31 -06:00
|
|
|
if let Some(braces) = mac_braces.macro_braces.get(name);
|
2022-10-06 02:44:38 -05:00
|
|
|
if let Some(snip) = snippet_opt(cx, span_call_site);
|
2021-07-15 03:44:10 -05:00
|
|
|
// we must check only invocation sites
|
|
|
|
// https://github.com/rust-lang/rust-clippy/issues/7422
|
2022-10-06 02:44:38 -05:00
|
|
|
if snip.starts_with(&format!("{name}!"));
|
2021-08-12 04:16:25 -05:00
|
|
|
if unnested_or_local();
|
2021-07-15 03:44:10 -05:00
|
|
|
// make formatting consistent
|
2021-12-06 05:33:31 -06:00
|
|
|
let c = snip.replace(' ', "");
|
2022-10-06 02:44:38 -05:00
|
|
|
if !c.starts_with(&format!("{name}!{}", braces.0));
|
|
|
|
if !mac_braces.done.contains(&span_call_site);
|
2021-07-01 11:17:38 -05:00
|
|
|
then {
|
2022-10-06 02:44:38 -05:00
|
|
|
Some((span_call_site, braces, snip))
|
2021-07-01 11:17:38 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-06 02:44:38 -05:00
|
|
|
fn emit_help(cx: &EarlyContext<'_>, snip: &str, braces: &(String, String), span: Span) {
|
|
|
|
if let Some((macro_name, macro_args_str)) = snip.split_once('!') {
|
|
|
|
let mut macro_args = macro_args_str.trim().to_string();
|
|
|
|
// now remove the wrong braces
|
|
|
|
macro_args.remove(0);
|
|
|
|
macro_args.pop();
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
NONSTANDARD_MACRO_BRACES,
|
|
|
|
span,
|
|
|
|
&format!("use of irregular braces for `{macro_name}!` macro"),
|
|
|
|
"consider writing",
|
|
|
|
format!("{macro_name}!{}{macro_args}{}", braces.0, braces.1),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
2021-07-01 11:17:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn macro_braces(conf: FxHashSet<MacroMatcher>) -> FxHashMap<String, (String, String)> {
|
|
|
|
let mut braces = vec![
|
|
|
|
macro_matcher!(
|
|
|
|
name: "print",
|
|
|
|
braces: ("(", ")"),
|
|
|
|
),
|
|
|
|
macro_matcher!(
|
|
|
|
name: "println",
|
|
|
|
braces: ("(", ")"),
|
|
|
|
),
|
|
|
|
macro_matcher!(
|
|
|
|
name: "eprint",
|
|
|
|
braces: ("(", ")"),
|
|
|
|
),
|
|
|
|
macro_matcher!(
|
|
|
|
name: "eprintln",
|
|
|
|
braces: ("(", ")"),
|
|
|
|
),
|
|
|
|
macro_matcher!(
|
|
|
|
name: "write",
|
|
|
|
braces: ("(", ")"),
|
|
|
|
),
|
|
|
|
macro_matcher!(
|
|
|
|
name: "writeln",
|
|
|
|
braces: ("(", ")"),
|
|
|
|
),
|
|
|
|
macro_matcher!(
|
|
|
|
name: "format",
|
|
|
|
braces: ("(", ")"),
|
|
|
|
),
|
|
|
|
macro_matcher!(
|
|
|
|
name: "format_args",
|
|
|
|
braces: ("(", ")"),
|
|
|
|
),
|
|
|
|
macro_matcher!(
|
|
|
|
name: "vec",
|
|
|
|
braces: ("[", "]"),
|
|
|
|
),
|
2022-09-21 12:02:37 -05:00
|
|
|
macro_matcher!(
|
|
|
|
name: "matches",
|
|
|
|
braces: ("(", ")"),
|
|
|
|
),
|
2021-07-01 11:17:38 -05:00
|
|
|
]
|
|
|
|
.into_iter()
|
|
|
|
.collect::<FxHashMap<_, _>>();
|
|
|
|
// We want users items to override any existing items
|
|
|
|
for it in conf {
|
|
|
|
braces.insert(it.name, it.braces);
|
|
|
|
}
|
|
|
|
braces
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! macro_matcher {
|
|
|
|
(name: $name:expr, braces: ($open:expr, $close:expr) $(,)?) => {
|
|
|
|
($name.to_owned(), ($open.to_owned(), $close.to_owned()))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
pub(crate) use macro_matcher;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct MacroMatcher {
|
|
|
|
name: String,
|
|
|
|
braces: (String, String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Hash for MacroMatcher {
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
self.name.hash(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq for MacroMatcher {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.name == other.name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Eq for MacroMatcher {}
|
|
|
|
|
|
|
|
impl<'de> Deserialize<'de> for MacroMatcher {
|
|
|
|
fn deserialize<D>(deser: D) -> Result<Self, D::Error>
|
|
|
|
where
|
|
|
|
D: de::Deserializer<'de>,
|
|
|
|
{
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(field_identifier, rename_all = "lowercase")]
|
|
|
|
enum Field {
|
|
|
|
Name,
|
|
|
|
Brace,
|
|
|
|
}
|
|
|
|
struct MacVisitor;
|
|
|
|
impl<'de> de::Visitor<'de> for MacVisitor {
|
|
|
|
type Value = MacroMatcher;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
formatter.write_str("struct MacroMatcher")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_map<V>(self, mut map: V) -> Result<Self::Value, V::Error>
|
|
|
|
where
|
|
|
|
V: de::MapAccess<'de>,
|
|
|
|
{
|
|
|
|
let mut name = None;
|
2023-06-02 04:41:57 -05:00
|
|
|
let mut brace: Option<String> = None;
|
2021-07-01 11:17:38 -05:00
|
|
|
while let Some(key) = map.next_key()? {
|
|
|
|
match key {
|
|
|
|
Field::Name => {
|
|
|
|
if name.is_some() {
|
|
|
|
return Err(de::Error::duplicate_field("name"));
|
|
|
|
}
|
|
|
|
name = Some(map.next_value()?);
|
|
|
|
},
|
|
|
|
Field::Brace => {
|
|
|
|
if brace.is_some() {
|
|
|
|
return Err(de::Error::duplicate_field("brace"));
|
|
|
|
}
|
|
|
|
brace = Some(map.next_value()?);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let name = name.ok_or_else(|| de::Error::missing_field("name"))?;
|
|
|
|
let brace = brace.ok_or_else(|| de::Error::missing_field("brace"))?;
|
|
|
|
Ok(MacroMatcher {
|
|
|
|
name,
|
|
|
|
braces: BRACES
|
|
|
|
.iter()
|
|
|
|
.find(|b| b.0 == brace)
|
|
|
|
.map(|(o, c)| ((*o).to_owned(), (*c).to_owned()))
|
2022-10-23 08:18:45 -05:00
|
|
|
.ok_or_else(|| de::Error::custom(format!("expected one of `(`, `{{`, `[` found `{brace}`")))?,
|
2021-07-01 11:17:38 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const FIELDS: &[&str] = &["name", "brace"];
|
|
|
|
deser.deserialize_struct("MacroMatcher", FIELDS, MacVisitor)
|
|
|
|
}
|
|
|
|
}
|