Skip to content

Commit

Permalink
Add more lints to check and clippy configuration (#345)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilai-deutel authored Nov 26, 2024
1 parent 7456225 commit 7bedad6
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 33 deletions.
12 changes: 9 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,19 @@ jobs:
components: rustfmt, clippy
- name: Format
if: ${{ matrix.target == 'x86_64-unknown-linux-gnu' }}
run: cargo fmt --check
run: cargo fmt --check --all
- name: Set up cargo-action-fmt
if: ${{ github.event_name == 'pull_request' && matrix.target == 'x86_64-unknown-linux-gnu' }}
uses: olix0r/cargo-action-fmt/setup@v2
- name: Check
if: ${{ github.event_name == 'pull_request' && matrix.target == 'x86_64-unknown-linux-gnu' }}
run: cargo check --all-features --all-targets --workspace --target ${{ matrix.target }} -q --message-format=json | cargo-action-fmt
- name: Check
if: ${{ github.event_name != 'pull_request' || matrix.target != 'x86_64-unknown-linux-gnu' }}
run: cargo check --all-features --all-targets --workspace --target ${{ matrix.target }}
- name: Clippy
if: ${{ github.event_name == 'pull_request' && matrix.target == 'x86_64-unknown-linux-gnu' }}
run: cargo clippy --all-features --all-targets --target ${{ matrix.target }} -q --message-format=json | cargo-action-fmt
run: cargo clippy --all-features --all-targets --workspace --target ${{ matrix.target }} -q --message-format=json | cargo-action-fmt
- name: Clippy
if: ${{ github.event_name != 'pull_request' || matrix.target != 'x86_64-unknown-linux-gnu' }}
run: cargo clippy --all-features --all-targets --target ${{ matrix.target }}
run: cargo clippy --all-features --all-targets --workspace --target ${{ matrix.target }}
83 changes: 74 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,78 @@ maintenance = { status = "actively-developed" }
[lints]
workspace = true

[workspace.lints.rust]
explicit_outlives_requirements = "deny"
let-underscore-drop = "deny"
meta-variable-misuse = "deny"
non_ascii_idents = "deny"
non-local-definitions = "deny"
redundant-imports = "deny"
redundant-lifetimes = "deny"
single-use-lifetimes = "deny"
trivial-casts = "deny"
trivial_numeric_casts = "deny"
unit-bindings = "deny"
unnameable-types = "deny"
unsafe-code = "deny" # Will only be allowed in platform-specific modules
unused-import-braces = "deny"
unused-lifetimes = "deny"
unused-macro-rules = "deny"
unused_qualifications = "deny"
variant_size_differences = "deny"

[workspace.lints.clippy]
correctness = "deny"
style = "deny"
complexity = "deny"
perf = "deny"
pedantic = "deny"
cargo = "deny"
suspicious = "warn"
nursery = "warn"
restriction = "allow"
correctness = { level = "deny", priority = -1 }
style = { level = "deny", priority = -1 }
complexity = { level = "deny", priority = -1 }
perf = { level = "deny", priority = -1 }
pedantic = { level = "deny", priority = -1 }
cargo = { level = "deny", priority = -1 }
suspicious = { level = "warn", priority = -1 }
nursery = { level = "warn", priority = -1 }

restriction = { level = "allow", priority = -1 }
allow_attributes = "deny"
as_underscore = "deny"
assertions_on_result_states = "deny"
cfg_not_test = "deny"
clone_on_ref_ptr = "deny"
create_dir = "deny"
dbg_macro = "deny"
empty_drop = "deny"
empty_enum_variants_with_brackets = "deny"
empty_structs_with_brackets = "deny"
exit = "deny"
expect_used = "deny"
float_cmp_const = "deny"
fn_to_numeric_cast_any = "deny"
format_push_string = "deny"
get_unwrap = "deny"
if_then_some_else_none = "deny"
impl_trait_in_params = "deny"
infinite_loop = "deny"
map_with_unused_argument_over_ranges = "deny"
mem_forget = "deny"
missing_assert_message = "deny"
mixed_read_write_in_expression = "deny"
mod_module_files = "deny"
module_name_repetitions = "deny"
multiple_inherent_impl = "deny"
mutex_atomic = "deny"
needless_raw_strings = "deny"
panic = "deny"
rc_buffer = "deny"
rc_mutex = "deny"
redundant_type_annotations = "deny"
ref_patterns = "deny"
renamed_function_params = "deny"
rest_pat_in_fully_bound_structs = "deny"
same_name_method = "deny"
semicolon_outside_block = "deny"
separated_literal_suffix = "deny"
str_to_string = "deny"
unused_result_ok = "deny"
unwrap_used = "deny"
use_debug = "deny"
verbose_file_reads = "deny"
wildcard_enum_match_arm = "deny"
3 changes: 3 additions & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
allow-expect-in-tests = true
allow-panic-in-tests = true
allow-unwrap-in-tests = true
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ mod tests {

impl Drop for TempEnvVar {
fn drop(&mut self) {
match self.orig_value {
Some(ref orig_value) => env::set_var(&self.key, orig_value),
match &self.orig_value {
Some(orig_value) => env::set_var(&self.key, orig_value),
None => env::remove_var(&self.key),
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ impl Editor {
self.file_name = None;
}
loop {
if let Some(ref mode) = self.prompt_mode {
if let Some(mode) = &self.prompt_mode {
set_status!(self, "{}", mode.status_msg());
}
self.refresh_screen()?;
Expand All @@ -740,6 +740,7 @@ impl Editor {
}

impl Drop for Editor {
#[allow(clippy::expect_used)]
/// When the editor is dropped, restore the original terminal mode.
fn drop(&mut self) {
if let Some(orig_term_mode) = self.orig_term_mode.take() {
Expand Down Expand Up @@ -792,6 +793,7 @@ impl PromptMode {
}
match process_prompt_keypress(b, key) {
PromptState::Active(query) => {
#[allow(clippy::wildcard_enum_match_arm)]
let (last_match, forward) = match key {
Key::Arrow(AKey::Right | AKey::Down) | Key::Char(FIND) =>
(last_match, true),
Expand Down Expand Up @@ -860,6 +862,7 @@ enum PromptState {

/// Process a prompt keypress event and return the new state for the prompt.
fn process_prompt_keypress(mut buffer: String, key: &Key) -> PromptState {
#[allow(clippy::wildcard_enum_match_arm)]
match key {
Key::Char(b'\r') => return PromptState::Completed(buffer),
Key::Escape | Key::Char(EXIT) => return PromptState::Cancelled,
Expand Down
4 changes: 2 additions & 2 deletions src/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ impl Row {
// The number of rendered characters
let n_rend_chars = if c == '\t' { tab - (rx % tab) } else { c.width().unwrap_or(1) };
self.render.push_str(&(if c == '\t' { " ".repeat(n_rend_chars) } else { c.into() }));
self.cx2rx.extend(std::iter::repeat(rx).take(c.len_utf8()));
self.rx2cx.extend(std::iter::repeat(cx).take(n_rend_chars));
self.cx2rx.extend(repeat(rx).take(c.len_utf8()));
self.rx2cx.extend(repeat(cx).take(n_rend_chars));
(rx, cx) = (rx + n_rend_chars, cx + c.len_utf8());
}
let (..) = (self.cx2rx.push(rx), self.rx2cx.push(cx));
Expand Down
2 changes: 1 addition & 1 deletion src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn get_window_size_using_cursor() -> Result<(usize, usize), Error> {
let mut stdin = sys::stdin()?;
print!("{REPOSITION_CURSOR_END}{DEVICE_STATUS_REPORT}");
io::stdout().flush()?;
let mut prefix_buffer = [0_u8; 2];
let mut prefix_buffer = [0u8; 2];
stdin.read_exact(&mut prefix_buffer)?;
if prefix_buffer != [b'\x1b', b'['] {
return Err(Error::CursorPosition);
Expand Down
4 changes: 3 additions & 1 deletion src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//!
//! UNIX-specific structs and functions. Will be imported as `sys` on UNIX
//! systems.
#![allow(unsafe_code)]

use std::sync::atomic::{AtomicBool, Ordering::Relaxed};

Expand Down Expand Up @@ -40,6 +41,7 @@ static WSC: AtomicBool = AtomicBool::new(false);
/// Handle a change in window size.
extern "C" fn handle_wsize(_: c_int, _: *mut siginfo_t, _: *mut c_void) { WSC.store(true, Relaxed) }

#[allow(clippy::fn_to_numeric_cast_any)]
/// Register a signal handler that sets a global variable when the window size
/// changes. After calling this function, use `has_window_size_changed` to query
/// the global variable.
Expand All @@ -51,7 +53,7 @@ pub fn register_winsize_change_signal_handler() -> Result<(), Error> {
// have sa_handler field, so we use sa_sigaction instead.
(*maybe_sa.as_mut_ptr()).sa_flags = SA_SIGINFO;
(*maybe_sa.as_mut_ptr()).sa_sigaction = handle_wsize as sighandler_t;
cerr(libc::sigaction(libc::SIGWINCH, maybe_sa.as_ptr(), std::ptr::null_mut()))
cerr(sigaction(libc::SIGWINCH, maybe_sa.as_ptr(), std::ptr::null_mut()))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use crate::Error;
pub use crate::xdg::*;

pub struct TermMode {}
pub struct TermMode;

/// Return the current window size as (rows, columns).
/// By returning an error we cause kibi to fall back to another method of
Expand Down
2 changes: 1 addition & 1 deletion src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ pub fn enable_raw_mode() -> Result<TermMode, Error> {
}

#[allow(clippy::unnecessary_wraps)] // Result required on other platforms
pub fn stdin() -> std::io::Result<std::io::Stdin> { Ok(std::io::stdin()) }
pub fn stdin() -> io::Result<io::Stdin> { Ok(io::stdin()) }

pub fn path(filename: &str) -> std::path::PathBuf { std::path::PathBuf::from(filename) }
10 changes: 5 additions & 5 deletions xtask/src/count_loc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::PathBuf;
use std::path::{Path, PathBuf};

use anstream::println;
use glob::glob;
Expand All @@ -23,10 +23,10 @@ pub fn count_loc() -> Result<()> {
print_summary(&results, longest_path, &["unix", "wasi", "windows"])
}

/// Filter out lines that contain `clippy` lints and anything after
/// Filter out lines that contain lints and anything after
/// `#[cfg(test)]` attributes.
pub fn filter_lines(path: &std::path::PathBuf) -> Result<String> {
let regex = Regex::new(r"^\s*#!?\[(?:allow|warn|deny)\(clippy::")?;
pub fn filter_lines(path: &Path) -> Result<String> {
let regex = Regex::new(r"^\s*#!?\[(?:allow|warn|deny)\(")?;
let content = std::fs::read_to_string(path)?;
let lines = content
.lines()
Expand Down Expand Up @@ -60,7 +60,7 @@ pub fn print_summary(results: &[(PathBuf, usize)], width: usize, platforms: &[&s
Ok(())
}

pub fn filter_count(results: &[(std::path::PathBuf, usize)], pattern: &str) -> usize {
pub fn filter_count(results: &[(PathBuf, usize)], pattern: &str) -> usize {
results
.iter()
.filter(|(path, _)| path.display().to_string().contains(pattern))
Expand Down
17 changes: 10 additions & 7 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::io::Write;
#![allow(clippy::multiple_crate_versions)]
use std::process::ExitCode;

use anstyle::{AnsiColor, Reset, Style};
use clap::{Parser, Subcommand};
Expand All @@ -11,12 +12,14 @@ const RESET: Reset = Reset;
const RED: Style = AnsiColor::Red.on_default();
const GREEN: Style = AnsiColor::Green.on_default();

fn main() {
fn main() -> ExitCode {
let args = Args::parse();
if let Err(err) = args.command.execute() {
let _ = std::io::stdout().flush();
eprintln!("{RED}{err}{RESET}");
std::process::exit(1);
match args.command.execute() {
Ok(()) => ExitCode::SUCCESS,
Err(err) => {
eprintln!("{RED}{err}{RESET}");
ExitCode::FAILURE
}
}
}

Expand All @@ -34,7 +37,7 @@ enum Command {
impl Command {
fn execute(&self) -> Result<()> {
match self {
Command::CountLoc => count_loc::count_loc(),
Self::CountLoc => count_loc::count_loc(),
}
}
}

0 comments on commit 7bedad6

Please sign in to comment.