Skip to content

Deprecate regex_macro lint #5760

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

Merged
merged 1 commit into from
Jul 1, 2020
Merged
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
10 changes: 9 additions & 1 deletion clippy_lints/src/deprecated_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,13 @@ declare_deprecated_lint! {
///
/// **Deprecation reason:** Associated-constants are now preferred.
pub REPLACE_CONSTS,
"associated-constants `MIN`/`MAX` of integers are prefer to `{min,max}_value()` and module constants"
"associated-constants `MIN`/`MAX` of integers are prefered to `{min,max}_value()` and module constants"
}

declare_deprecated_lint! {
/// **What it does:** Nothing. This lint has been deprecated.
///
/// **Deprecation reason:** The regex! macro does not exist anymore.
pub REGEX_MACRO,
"the regex! macro has been removed from the regex crate in 2018"
}
9 changes: 5 additions & 4 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,11 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
);
store.register_removed(
"clippy::replace_consts",
"associated-constants `MIN`/`MAX` of integers are prefer to `{min,max}_value()` and module constants",
"associated-constants `MIN`/`MAX` of integers are prefered to `{min,max}_value()` and module constants",
);
store.register_removed(
"clippy::regex_macro",
"the regex! macro has been removed from the regex crate in 2018",
);
// end deprecated lints, do not remove this comment, it’s used in `update_lints`

Expand Down Expand Up @@ -755,7 +759,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
&reference::DEREF_ADDROF,
&reference::REF_IN_DEREF,
&regex::INVALID_REGEX,
&regex::REGEX_MACRO,
&regex::TRIVIAL_REGEX,
&returns::NEEDLESS_RETURN,
&returns::UNUSED_UNIT,
Expand Down Expand Up @@ -1380,7 +1383,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&reference::DEREF_ADDROF),
LintId::of(&reference::REF_IN_DEREF),
LintId::of(&regex::INVALID_REGEX),
LintId::of(&regex::REGEX_MACRO),
LintId::of(&regex::TRIVIAL_REGEX),
LintId::of(&returns::NEEDLESS_RETURN),
LintId::of(&returns::UNUSED_UNIT),
Expand Down Expand Up @@ -1517,7 +1519,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&redundant_field_names::REDUNDANT_FIELD_NAMES),
LintId::of(&redundant_pattern_matching::REDUNDANT_PATTERN_MATCHING),
LintId::of(&redundant_static_lifetimes::REDUNDANT_STATIC_LIFETIMES),
LintId::of(&regex::REGEX_MACRO),
LintId::of(&regex::TRIVIAL_REGEX),
LintId::of(&returns::NEEDLESS_RETURN),
LintId::of(&returns::UNUSED_UNIT),
Expand Down
57 changes: 3 additions & 54 deletions clippy_lints/src/regex.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::consts::{constant, Constant};
use crate::utils::{is_expn_of, match_def_path, match_type, paths, span_lint, span_lint_and_help};
use crate::utils::{match_def_path, paths, span_lint, span_lint_and_help};
use if_chain::if_chain;
use rustc_ast::ast::{LitKind, StrStyle};
use rustc_data_structures::fx::FxHashSet;
use rustc_hir::{Block, BorrowKind, Crate, Expr, ExprKind, HirId};
use rustc_hir::{BorrowKind, Expr, ExprKind, HirId};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::source_map::{BytePos, Span};
Expand Down Expand Up @@ -46,66 +46,15 @@ declare_clippy_lint! {
"trivial regular expressions"
}

declare_clippy_lint! {
/// **What it does:** Checks for usage of `regex!(_)` which (as of now) is
/// usually slower than `Regex::new(_)` unless called in a loop (which is a bad
/// idea anyway).
///
/// **Why is this bad?** Performance, at least for now. The macro version is
/// likely to catch up long-term, but for now the dynamic version is faster.
///
/// **Known problems:** None.
///
/// **Example:**
/// ```ignore
/// regex!("foo|bar")
/// ```
pub REGEX_MACRO,
style,
"use of `regex!(_)` instead of `Regex::new(_)`"
}

#[derive(Clone, Default)]
pub struct Regex {
spans: FxHashSet<Span>,
last: Option<HirId>,
}

impl_lint_pass!(Regex => [INVALID_REGEX, REGEX_MACRO, TRIVIAL_REGEX]);
impl_lint_pass!(Regex => [INVALID_REGEX, TRIVIAL_REGEX]);

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Regex {
fn check_crate(&mut self, _: &LateContext<'a, 'tcx>, _: &'tcx Crate<'_>) {
self.spans.clear();
}

fn check_block(&mut self, cx: &LateContext<'a, 'tcx>, block: &'tcx Block<'_>) {
if_chain! {
if self.last.is_none();
if let Some(ref expr) = block.expr;
if match_type(cx, cx.tables().expr_ty(expr), &paths::REGEX);
if let Some(span) = is_expn_of(expr.span, "regex");
then {
if !self.spans.contains(&span) {
span_lint(
cx,
REGEX_MACRO,
span,
"`regex!(_)` found. \
Please use `Regex::new(_)`, which is faster for now."
);
self.spans.insert(span);
}
self.last = Some(block.hir_id);
}
}
}

fn check_block_post(&mut self, _: &LateContext<'a, 'tcx>, block: &'tcx Block<'_>) {
if self.last.map_or(false, |id| block.hir_id == id) {
self.last = None;
}
}

fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
if_chain! {
if let ExprKind::Call(ref fun, ref args) = expr.kind;
Expand Down
1 change: 0 additions & 1 deletion clippy_lints/src/utils/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ pub const RANGE_TO_STD: [&str; 3] = ["std", "ops", "RangeTo"];
pub const RC: [&str; 3] = ["alloc", "rc", "Rc"];
pub const RC_PTR_EQ: [&str; 4] = ["alloc", "rc", "Rc", "ptr_eq"];
pub const RECEIVER: [&str; 4] = ["std", "sync", "mpsc", "Receiver"];
pub const REGEX: [&str; 3] = ["regex", "re_unicode", "Regex"];
pub const REGEX_BUILDER_NEW: [&str; 5] = ["regex", "re_builder", "unicode", "RegexBuilder", "new"];
pub const REGEX_BYTES_BUILDER_NEW: [&str; 5] = ["regex", "re_builder", "bytes", "RegexBuilder", "new"];
pub const REGEX_BYTES_NEW: [&str; 4] = ["regex", "re_bytes", "Regex", "new"];
Expand Down
7 changes: 0 additions & 7 deletions src/lintlist/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1865,13 +1865,6 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
deprecation: None,
module: "reference",
},
Lint {
name: "regex_macro",
group: "style",
desc: "use of `regex!(_)` instead of `Regex::new(_)`",
deprecation: None,
module: "regex",
},
Lint {
name: "rest_pat_in_fully_bound_structs",
group: "restriction",
Expand Down
1 change: 1 addition & 0 deletions tests/ui/deprecated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
#[warn(clippy::invalid_ref)]
#[warn(clippy::into_iter_on_array)]
#[warn(clippy::unused_label)]
#[warn(clippy::regex_macro)]

fn main() {}
8 changes: 7 additions & 1 deletion tests/ui/deprecated.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,17 @@ error: lint `clippy::unused_label` has been removed: `this lint has been uplifte
LL | #[warn(clippy::unused_label)]
| ^^^^^^^^^^^^^^^^^^^^

error: lint `clippy::regex_macro` has been removed: `the regex! macro has been removed from the regex crate in 2018`
--> $DIR/deprecated.rs:10:8
|
LL | #[warn(clippy::regex_macro)]
| ^^^^^^^^^^^^^^^^^^^

error: lint `clippy::str_to_string` has been removed: `using `str::to_string` is common even today and specialization will likely happen soon`
--> $DIR/deprecated.rs:1:8
|
LL | #[warn(clippy::str_to_string)]
| ^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 10 previous errors
error: aborting due to 11 previous errors

2 changes: 1 addition & 1 deletion tests/ui/regex.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![allow(unused)]
#![warn(clippy::invalid_regex, clippy::trivial_regex, clippy::regex_macro)]
#![warn(clippy::invalid_regex, clippy::trivial_regex)]

extern crate regex;

Expand Down