Skip to content

Commit

Permalink
fix dylint
Browse files Browse the repository at this point in the history
format_args: rust-lang/rust-clippy#12567

Signed-off-by: xxchan <[email protected]>
  • Loading branch information
xxchan committed Jul 31, 2024
1 parent 39bded2 commit 71a0609
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 29 deletions.
8 changes: 4 additions & 4 deletions lints/Cargo.lock

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

2 changes: 1 addition & 1 deletion lints/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ path = "ui/format_error.rs"
# See `README.md` before bumping the version.
# Remember to update the version in `ci/Dockerfile` as well.
[dependencies]
clippy_utils = { git = "https://github.com/rust-lang/rust-clippy", rev = "fca4e16ffb8c07186ee23becd44cd5c9fb51896c" }
clippy_utils = { git = "https://github.com/rust-lang/rust-clippy", rev = "35f54fd439432f56c749fbb34f1326e34ed1fc42" }
dylint_linting = "3.1.0"
itertools = "0.12"

Expand Down
9 changes: 7 additions & 2 deletions lints/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ Duplicate `.vscode/settings.json.example` to `.vscode/settings.json` to enable r

## Bump toolchain

The version of the toolchain is specified in `rust-toolchain` file under current directory.
It does not have to be exactly the same as the one used to build RisingWave, but it should be close enough to avoid compile errors.
The version of the toolchain is specified in `rust-toolchain` file under current directory. It will be used to build the lints, and also be used by `dylint` to compile RisingWave, instead of the root-level `rust-toolchain`.

So the chosen toolchain needs to
1. be close enough to the root-level `rust-toolchain` to make RisingWave compile. It does not have to be exactly the same version though.
2. be close enough to the dependency `clippy_utils`'s corresponding `rust-toolchain` in the Clippy's repo.

(Note: `clippy_utils` depends on rustc's internal unstable API. When rustc has breaking changes, the `rust` repo's Clippy will be updated. And then it's [synced back to the Clippy repo bi-weekly](https://doc.rust-lang.org/clippy/development/infrastructure/sync.html#syncing-changes-between-clippy-and-rust-langrust). So ideally we can use `clippy_utils` in the rust repo corresponding to our root-level nightly version, but that repo is too large. Perhaps we can also consider copy the code out to workaround this problem.)

The information below can be helpful in finding the appropriate version to bump to.

Expand Down
2 changes: 1 addition & 1 deletion lints/rust-toolchain
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# See `README.md` before bumping the version.

[toolchain]
channel = "nightly-2024-06-08"
channel = "nightly-2024-06-12"
components = ["llvm-tools-preview", "rustc-dev"]
14 changes: 11 additions & 3 deletions lints/src/format_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::macros::{
find_format_arg_expr, find_format_args, is_format_macro, macro_backtrace,
find_format_arg_expr, is_format_macro, macro_backtrace, FormatArgsStorage,
};
use clippy_utils::ty::{implements_trait, match_type};
use clippy_utils::{
Expand Down Expand Up @@ -56,7 +56,15 @@ declare_tool_lint! {
}

#[derive(Default)]
pub struct FormatError;
pub struct FormatError {
format_args: FormatArgsStorage,
}

impl FormatError {
pub fn new(format_args: FormatArgsStorage) -> Self {
Self { format_args }
}
}

impl_lint_pass!(FormatError => [FORMAT_ERROR]);

Expand Down Expand Up @@ -90,7 +98,7 @@ impl<'tcx> LateLintPass<'tcx> for FormatError {

for macro_call in macro_backtrace(expr.span) {
if is_format_macro(cx, macro_call.def_id)
&& let Some(format_args) = find_format_args(cx, expr, macro_call.expn)
&& let Some(format_args) = self.format_args.get(cx, expr, macro_call.expn)
{
for piece in &format_args.template {
if let FormatArgsPiece::Placeholder(placeholder) = piece
Expand Down
13 changes: 9 additions & 4 deletions lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

#![feature(rustc_private)]
#![feature(let_chains)]
#![feature(lazy_cell)]
#![warn(unused_extern_crates)]

extern crate rustc_ast;
Expand All @@ -36,13 +35,19 @@ pub fn register_lints(_sess: &rustc_session::Session, lint_store: &mut rustc_lin
// -- Begin lint registration --

// Preparation steps.
lint_store.register_early_pass(|| {
Box::<utils::format_args_collector::FormatArgsCollector>::default()
let format_args_storage = clippy_utils::macros::FormatArgsStorage::default();
let format_args = format_args_storage.clone();
lint_store.register_early_pass(move || {
Box::new(utils::format_args_collector::FormatArgsCollector::new(
format_args.clone(),
))
});

// Actual lints.
lint_store.register_lints(&[format_error::FORMAT_ERROR]);
lint_store.register_late_pass(|_| Box::<format_error::FormatError>::default());
let format_args = format_args_storage.clone();
lint_store
.register_late_pass(move |_| Box::new(format_error::FormatError::new(format_args.clone())));

// -- End lint registration --

Expand Down
30 changes: 16 additions & 14 deletions lints/src/utils/format_args_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//! Copied from `https://github.com/rust-lang/rust-clippy/blob/8b0bf6423dfaf5545014db85fcba7bc745beed4c/clippy_lints/src/utils/format_args_collector.rs`
//!
//! Init `AST_FORMAT_ARGS` before running the late pass, so that we can call `find_format_args`.
//! Copied from `https://github.com/Alexendoo/rust-clippy/blob/c187bff864234e869dabcb41d2336639e29e2511/clippy_lints/src/utils/format_args_collector.rs`
use std::iter::once;
use std::mem;
use std::rc::Rc;

use clippy_utils::macros::AST_FORMAT_ARGS;
use clippy_utils::macros::FormatArgsStorage;
use clippy_utils::source::snippet_opt;
use itertools::Itertools;
use rustc_ast::{Crate, Expr, ExprKind, FormatArgs};
Expand All @@ -30,11 +27,19 @@ use rustc_lint::{EarlyContext, EarlyLintPass};
use rustc_session::impl_lint_pass;
use rustc_span::{hygiene, Span};

/// Collects [`rustc_ast::FormatArgs`] so that future late passes can call
/// [`clippy_utils::macros::find_format_args`]
#[derive(Default)]
/// Populates [`FormatArgsStorage`] with AST [`FormatArgs`] nodes
pub struct FormatArgsCollector {
format_args: FxHashMap<Span, Rc<FormatArgs>>,
format_args: FxHashMap<Span, FormatArgs>,
storage: FormatArgsStorage,
}

impl FormatArgsCollector {
pub fn new(storage: FormatArgsStorage) -> Self {
Self {
format_args: FxHashMap::default(),
storage,
}
}
}

impl_lint_pass!(FormatArgsCollector => []);
Expand All @@ -47,15 +52,12 @@ impl EarlyLintPass for FormatArgsCollector {
}

self.format_args
.insert(expr.span.with_parent(None), Rc::new((**args).clone()));
.insert(expr.span.with_parent(None), (**args).clone());
}
}

fn check_crate_post(&mut self, _: &EarlyContext<'_>, _: &Crate) {
AST_FORMAT_ARGS.with(|ast_format_args| {
let result = ast_format_args.set(mem::take(&mut self.format_args));
debug_assert!(result.is_ok());
});
self.storage.set(mem::take(&mut self.format_args));
}
}

Expand Down

0 comments on commit 71a0609

Please sign in to comment.