Skip to content
Open
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
2 changes: 1 addition & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
RUSTC_BOOTSTRAP = "1"

[build]
rustflags = ["-Clinker-features=-lld"]
rustflags = ["-Zunstable-options", "-Clinker-features=-lld"]
23 changes: 23 additions & 0 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ license = "MIT OR Apache-2.0"
default-run = "klint"

[dependencies]
rusqlite = "0.37"
rusqlite = { version = "0.37", features = ["bundled"] }
home = "0.5"
object = "0.38.0"
gimli = "0.32.3"
Expand Down
1 change: 1 addition & 0 deletions src/hir_lints/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub(crate) mod not_using_prelude;
pub(crate) mod pr_missing_newline;
63 changes: 63 additions & 0 deletions src/hir_lints/pr_missing_newline.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use rustc_ast::{MacCall, token};
use rustc_ast::tokenstream::TokenTree;
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
use rustc_session::{declare_tool_lint, impl_lint_pass};

declare_tool_lint! {
/// The `pr_missing_newline` lint detects `pr_*` calls that do not end with a newline.
pub klint::PR_MISSING_NEWLINE,
Warn,
"pr_* logging calls should end with a trailing \"\\n\""
}

pub struct PrMissingNewline;

impl_lint_pass!(PrMissingNewline => [PR_MISSING_NEWLINE]);

impl EarlyLintPass for PrMissingNewline {
fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &MacCall) {
// Check if the macro path starts with "pr_"
if let Some(segment) = mac.path.segments.last() {
let name = segment.ident.as_str();
if !name.starts_with("pr_") {
return;
}
if name == "pr_cont" {
return;
}
} else {
return;
}

// We want to check the first argument of the macro.
// The arguments are in `mac.args`. This is a `MacArgs`.
// We usually expect `MacArgs::Delimited`.

// `mac.args` appears to be `P<DelimArgs>` in this toolchain.
let tokens = &mac.args.tokens;

// We need to look at the tokens to find the format string.
// This is a simplified check assuming the first token is a string literal.
// A robust check might need to parse the token stream, but for `pr_info!("str")`
// checking the first token tree is often enough if we skip whitespace/comments?
// Actually, `rustc_ast` usually gives us a stream.

// We need to look at the tokens to find the format string.
for tt in tokens.iter() {
if let TokenTree::Token(token, _) = tt {
if let token::TokenKind::Literal(lit) = token.kind {
if matches!(lit.kind, token::LitKind::Str | token::LitKind::StrRaw(_)) {
let msg = lit.symbol.as_str();
if !msg.ends_with('\n') {
cx.span_lint(PR_MISSING_NEWLINE, mac.span(), |diag| {
diag.primary_message("pr_* logging calls should end with a trailing \"\\n\"");
});
}
// We found the format string, stop checking.
return;
}
}
}
}
}
}
1 change: 1 addition & 0 deletions src/infallible_allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fn is_generic_fn<'tcx>(instance: Instance<'tcx>) -> bool {
}

impl<'tcx> LateLintPass<'tcx> for InfallibleAllocation {
#[allow(rustc::potential_query_instability)]
fn check_crate(&mut self, cx: &LateContext<'tcx>) {
// Collect all mono items to be codegened with this crate. Discard the inline map, it does
// not contain enough information for us; we will collect them ourselves later.
Expand Down
7 changes: 7 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#![feature(unsize)]
#![warn(rustc::internal)]

#![allow(rustc::implicit_sysroot_crate_import)]

#[macro_use]
extern crate rustc_macros;
#[macro_use]
Expand Down Expand Up @@ -121,6 +123,7 @@ impl Callbacks for MyCallbacks {
atomic_context::ATOMIC_CONTEXT,
binary_analysis::stack_size::STACK_FRAME_TOO_LARGE,
hir_lints::not_using_prelude::NOT_USING_PRELUDE,
hir_lints::pr_missing_newline::PR_MISSING_NEWLINE,
]);

lint_store.register_late_pass(|tcx| {
Expand All @@ -129,6 +132,10 @@ impl Callbacks for MyCallbacks {
})
});

lint_store.register_early_pass(|| {
Box::new(hir_lints::pr_missing_newline::PrMissingNewline)
});

// lint_store
// .register_late_pass(|_| Box::new(infallible_allocation::InfallibleAllocation));
lint_store.register_late_pass(|tcx| {
Expand Down
1 change: 1 addition & 0 deletions src/monomorphize_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ impl<'tcx> UsageMap<'tcx> {
}

// Internally iterate over all items and the things each accesses.
#[allow(rustc::potential_query_instability)]
pub fn for_each_item_and_its_used_items<F>(&self, mut f: F)
where
F: FnMut(MonoItem<'tcx>, &[Spanned<MonoItem<'tcx>>]),
Expand Down
12 changes: 9 additions & 3 deletions src/rustdoc.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
use std::io::Result;
#[cfg(unix)]
use std::os::unix::process::CommandExt;

fn main() -> Result<()> {
Err(std::process::Command::new(env!("RUSTDOC"))
.args(std::env::args().skip(1))
.exec())
let mut cmd = std::process::Command::new(env!("RUSTDOC"));
cmd.args(std::env::args().skip(1));

#[cfg(unix)]
return Err(cmd.exec());

#[cfg(not(unix))]
std::process::exit(cmd.status()?.code().unwrap_or(1));
}
20 changes: 20 additions & 0 deletions tests/ui/pr_missing_newline.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// check-pass


#[macro_export]
macro_rules! pr_info {
($($arg:tt)*) => {};
}

#[macro_export]
macro_rules! pr_cont {
($($arg:tt)*) => {};
}

fn main() {
pr_info!("hello"); //~ WARN pr_* logging calls should end with a trailing "\n"
pr_info!("hello\n");
pr_cont!("hello");
pr_info!("hello {}", "world"); //~ WARN pr_* logging calls should end with a trailing "\n"
pr_info!("hello {}\n", "world");
}