-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add lint to handle deprecated rayon methods
- Loading branch information
Showing
9 changed files
with
242 additions
and
30 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
Oops, something went wrong.