Skip to content

Commit

Permalink
add lint to handle deprecated rayon methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lucarlig committed Mar 25, 2024
1 parent 6043bc1 commit 9fdd2e6
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 30 deletions.
70 changes: 41 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ fold = { path = "lints/fold", features = ["rlib"] }
par_fold = { path = "lints/par_fold", features = ["rlib"] }
par_iter = { path = "lints/par_iter", features = ["rlib"] }
rayon_imports = { path = "lints/rayon_imports", features = ["rlib"] }
deprecated_rayon = { path = "lints/deprecated_rayon", features = ["rlib"] }


dylint_linting = { version = "3.0.0" }

Expand All @@ -29,13 +31,14 @@ rustc_private = true

[workspace]
members = [
"lints/rayon_imports",
"lints/for_each",
"lints/filter",
"lints/map",
"lints/fold",
"lints/par_fold",
"lints/par_iter",
"lints/rayon_imports",
"lints/deprecated_rayon",
"utils",
]

Expand Down
1 change: 1 addition & 0 deletions lints/deprecated_rayon/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
34 changes: 34 additions & 0 deletions lints/deprecated_rayon/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[package]
name = "deprecated_rayon"
version = "0.1.1"
authors = ["authors go here"]
description = "description goes here"
edition = "2021"
publish = false

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
dylint_linting = "3.0.0"

clippy_utils = { workspace = true }
utils = { workspace = true }

[dev-dependencies]
dylint_testing = "3.0.0"
rayon = "1.9.0"

[package.metadata.rust-analyzer]
rustc_private = true

[features]
rlib = ["dylint_linting/constituent"]

[[example]]
name = "deprecated_main"
path = "ui/main.rs"


[lints]
workspace = true
17 changes: 17 additions & 0 deletions lints/deprecated_rayon/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# template

### What it does

### Why is this bad?

### Known problems
Remove if none.

### Example
```rust
// example code where a warning is issued
```
Use instead:
```rust
// example code that does not raise a warning
```
82 changes: 82 additions & 0 deletions lints/deprecated_rayon/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#![feature(rustc_private)]
#![warn(unused_extern_crates)]
#![feature(let_chains)]

extern crate rustc_errors;
extern crate rustc_hir;

use clippy_utils::get_parent_expr;
use rustc_errors::Applicability;
use rustc_hir::{self as hir};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use utils::span_to_snippet_macro;

dylint_linting::declare_late_lint! {
/// ### What it does
///
/// ### Why is this bad?
///
/// ### Known problems
///
///
/// ### Example
/// ```rust
///
/// ```
/// Use instead:
/// ```rust
///
///
///
/// ```
pub DEPRECATED_RAYON,
Warn,
"suggest replacing a deprecated method"
}

impl<'tcx> LateLintPass<'tcx> for DeprecatedRayon {
// TODO: implement check crate to check if rayon is present
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
if let hir::ExprKind::MethodCall(path, recv, _args, _span) = &expr.kind
&& ["par_iter", "into+par_iter", "par_iter_mut"].contains(&path.ident.as_str())
{
let mut top_expr = *recv;
while let Some(parent_expr) = get_parent_expr(cx, top_expr) {
match parent_expr.kind {
hir::ExprKind::MethodCall(method_name, _, _, _) => {
top_expr = parent_expr;
let snippet =
span_to_snippet_macro(cx.sess().source_map(), parent_expr.span);
let suggestion_text = match method_name.ident.as_str() {
"find" => Some(snippet.replace(".find(", ".find_first(")),
"position" => Some(snippet.replace(".position(", ".position_first(")),
_ => None,
};

if let Some(suggestion) = suggestion_text {
cx.span_lint(
DEPRECATED_RAYON,
top_expr.span,
"found a deprecated rayon method",
|diag| {
diag.span_suggestion(
parent_expr.span,
"try use this instead",
suggestion,
Applicability::MachineApplicable,
);
},
);
}
}
_ => break,
}
}
}
}
}

#[test]
fn ui() {
dylint_testing::ui_test_examples(env!("CARGO_PKG_NAME"));
}
19 changes: 19 additions & 0 deletions lints/deprecated_rayon/ui/main.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// run-rustfix
#![allow(dead_code, unused_variables, deprecated)]

use rayon::prelude::*;

fn main() {}

fn find_simple() {
let bufs = vec![vec![1], vec![2], vec![2]];
let buf = bufs
.par_iter()
.find_first(|b| !b.is_empty())
.map_or(&[][..], |b| &**b);
}

fn position_simple() {
let bufs = vec![vec![1], vec![2], vec![2]];
let buf = bufs.par_iter().position_first(|b| !b.is_empty());
}
Loading

0 comments on commit 9fdd2e6

Please sign in to comment.