Skip to content

Commit 7d4d326

Browse files
committed
Use workspace to set clippy linters
use them in yjit and zjit fix clippy issues
1 parent fccd96c commit 7d4d326

File tree

16 files changed

+124
-109
lines changed

16 files changed

+124
-109
lines changed

Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,19 @@ overflow-checks = true
4949
debug = true
5050
# Use ThinLTO. Much smaller output for a small amount of build time increase.
5151
lto = "thin"
52+
53+
[workspace.lints.clippy]
54+
correctness = { level = "forbid", priority = 1 }
55+
suspicious = { level = "forbid", priority = 1 }
56+
perf = { level = "forbid", priority = 1 }
57+
58+
complexity = { level = "deny", priority = -1 }
59+
60+
style = { level = "allow", priority = -1 }
61+
pedantic = { level = "allow", priority = -1 }
62+
63+
too_many_arguments = "allow"
64+
identity_op = "allow"
65+
66+
unwrap_or_default = "forbid"
67+
ptr_arg = "forbid"

yjit/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ disasm = ["capstone"]
2424
# from cfg!(debug_assertions) so that we can see disasm of the code
2525
# that would run in the release mode.
2626
runtime_checks = []
27+
28+
[lints]
29+
workspace = true

yjit/src/asm/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -224,16 +224,16 @@ impl CodeBlock {
224224
}
225225

226226
/// Free the memory pages of given code page indexes
227-
fn free_pages(&mut self, page_idxs: &Vec<usize>) {
228-
let mut page_idxs = page_idxs.clone();
229-
page_idxs.reverse(); // to loop with pop()
227+
/// In Rust >= 1.77 this could probably be simplified with chunk_by.
228+
fn free_pages(&mut self, page_idxs: &[usize]) {
229+
let mut page_idxs = page_idxs.iter().rev().collect::<Vec<_>>();
230230

231231
// Group adjacent page indexes and free them in batches to reduce the # of syscalls.
232-
while let Some(page_idx) = page_idxs.pop() {
232+
while let Some(&page_idx) = page_idxs.pop() {
233233
// Group first adjacent page indexes
234234
let mut batch_idxs = vec![page_idx];
235-
while page_idxs.last() == Some(&(batch_idxs.last().unwrap() + 1)) {
236-
batch_idxs.push(page_idxs.pop().unwrap());
235+
while page_idxs.last() == Some(&&(*batch_idxs.last().unwrap() + 1)) {
236+
batch_idxs.push(*page_idxs.pop().unwrap());
237237
}
238238

239239
// Free the grouped pages at once
@@ -441,12 +441,12 @@ impl CodeBlock {
441441

442442
// Ignore empty code ranges
443443
if start_addr == end_addr {
444-
return (0..0).into_iter();
444+
return 0..0;
445445
}
446446

447447
let start_page = (start_addr.raw_addr(self) - mem_start) / self.page_size;
448448
let end_page = (end_addr.raw_addr(self) - mem_start - 1) / self.page_size;
449-
(start_page..end_page + 1).into_iter()
449+
start_page..end_page + 1
450450
}
451451

452452
/// Get a (possibly dangling) direct pointer to the current write position

yjit/src/codegen.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type InsnGenFn = fn(
4343

4444
/// Ephemeral code generation state.
4545
/// Represents a [crate::core::Block] while we build it.
46+
#[allow(clippy::type_complexity)]
4647
pub struct JITState<'a> {
4748
/// Instruction sequence for the compiling block
4849
pub iseq: IseqPtr,
@@ -1178,14 +1179,14 @@ pub fn gen_entry_reg_mapping(asm: &mut Assembler, blockid: BlockId, stack_size:
11781179
// Find an existing callee block. If it's not found or uses no register, skip loading registers.
11791180
let mut ctx = Context::default();
11801181
ctx.set_stack_size(stack_size);
1181-
let reg_mapping = find_most_compatible_reg_mapping(blockid, &ctx).unwrap_or(RegMapping::default());
1182+
let reg_mapping = find_most_compatible_reg_mapping(blockid, &ctx).unwrap_or_default();
11821183
if reg_mapping == RegMapping::default() {
11831184
return reg_mapping;
11841185
}
11851186

11861187
// If found, load the same registers to reuse the block.
11871188
asm_comment!(asm, "reuse maps: {:?}", reg_mapping);
1188-
let local_table_size: u32 = unsafe { get_iseq_body_local_table_size(blockid.iseq) }.try_into().unwrap();
1189+
let local_table_size: u32 = unsafe { get_iseq_body_local_table_size(blockid.iseq) };
11891190
for &reg_opnd in reg_mapping.get_reg_opnds().iter() {
11901191
match reg_opnd {
11911192
RegOpnd::Local(local_idx) => {
@@ -7708,7 +7709,7 @@ fn gen_send_iseq(
77087709
// runtime guards later in copy_splat_args_for_rest_callee()
77097710
if !iseq_has_rest {
77107711
let supplying = argc - 1 - i32::from(kw_splat) + array_length as i32;
7711-
if (required_num..=required_num + opt_num).contains(&supplying) == false {
7712+
if !(required_num..=required_num + opt_num).contains(&supplying) {
77127713
gen_counter_incr(jit, asm, Counter::send_iseq_splat_arity_error);
77137714
return None;
77147715
}

yjit/src/core.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,7 @@ impl Context {
10991099
MapToLocal(local_idx) => {
11001100
bits.push_op(CtxOp::MapTempLocal);
11011101
bits.push_u3(stack_idx as u8);
1102-
bits.push_u3(local_idx as u8);
1102+
bits.push_u3(local_idx);
11031103
}
11041104

11051105
MapToSelf => {
@@ -1170,19 +1170,19 @@ impl Context {
11701170

11711171
match op {
11721172
CtxOp::SetSelfType => {
1173-
ctx.self_type = unsafe { transmute(bits.read_u4(&mut idx)) };
1173+
ctx.self_type = unsafe { transmute::<u8, Type>(bits.read_u4(&mut idx)) };
11741174
}
11751175

11761176
CtxOp::SetLocalType => {
11771177
let local_idx = bits.read_u3(&mut idx) as usize;
1178-
let t = unsafe { transmute(bits.read_u4(&mut idx)) };
1178+
let t = unsafe { transmute::<u8, Type>(bits.read_u4(&mut idx)) };
11791179
ctx.set_local_type(local_idx, t);
11801180
}
11811181

11821182
// Map temp to stack (known type)
11831183
CtxOp::SetTempType => {
11841184
let temp_idx = bits.read_u3(&mut idx) as usize;
1185-
let temp_type = unsafe { transmute(bits.read_u4(&mut idx)) };
1185+
let temp_type = unsafe { transmute::<u8, Type>(bits.read_u4(&mut idx)) };
11861186
ctx.set_temp_mapping(temp_idx, TempMapping::MapToStack(temp_type));
11871187
}
11881188

@@ -2294,7 +2294,7 @@ pub fn limit_block_versions(blockid: BlockId, ctx: &Context) -> Context {
22942294
let generic_ctx = ctx.get_generic_ctx();
22952295

22962296
if cfg!(debug_assertions) {
2297-
let mut ctx = ctx.clone();
2297+
let mut ctx = *ctx;
22982298
if ctx.inline() {
22992299
// Suppress TypeDiff::Incompatible from ctx.diff(). We return TypeDiff::Incompatible
23002300
// to keep inlining blocks until we hit the limit, but it's safe to give up inlining.
@@ -2920,7 +2920,7 @@ impl Context {
29202920
}
29212921

29222922
// Prepare a Context with the same registers
2923-
let mut dst_with_same_regs = dst.clone();
2923+
let mut dst_with_same_regs = *dst;
29242924
dst_with_same_regs.set_reg_mapping(self.get_reg_mapping());
29252925

29262926
// Diff registers and other stuff separately, and merge them
@@ -3823,7 +3823,7 @@ pub fn gen_branch_stub_hit_trampoline(ocb: &mut OutlinedCb) -> Option<CodePtr> {
38233823
}
38243824

38253825
/// Return registers to be pushed and popped on branch_stub_hit.
3826-
pub fn caller_saved_temp_regs() -> impl Iterator<Item = &'static Reg> + DoubleEndedIterator {
3826+
pub fn caller_saved_temp_regs() -> impl DoubleEndedIterator<Item = &'static Reg> {
38273827
let temp_regs = Assembler::get_temp_regs().iter();
38283828
let len = temp_regs.len();
38293829
// The return value gen_leave() leaves in C_RET_REG

yjit/src/cruby.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ pub type size_t = u64;
9797
pub type RedefinitionFlag = u32;
9898

9999
#[allow(dead_code)]
100-
#[allow(clippy::all)]
100+
#[allow(clippy::complexity)]
101101
mod autogened {
102102
use super::*;
103103
// Textually include output from rust-bindgen as suggested by its user guide.

yjit/src/lib.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
// Clippy disagreements
2-
#![allow(clippy::style)] // We are laid back about style
3-
#![allow(clippy::too_many_arguments)] // :shrug:
4-
#![allow(clippy::identity_op)] // Sometimes we do it for style
5-
61
// TODO(alan): This lint is right -- the way we use `static mut` is UB happy. We have many globals
72
// and take `&mut` frequently, sometimes with a method that easily allows calling it twice.
83
//

yjit/src/utils.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,7 @@ pub fn ruby_str_to_rust(v: VALUE) -> String {
9292
let str_ptr = unsafe { rb_RSTRING_PTR(v) } as *mut u8;
9393
let str_len: usize = unsafe { rb_RSTRING_LEN(v) }.try_into().unwrap();
9494
let str_slice: &[u8] = unsafe { slice::from_raw_parts(str_ptr, str_len) };
95-
match String::from_utf8(str_slice.to_vec()) {
96-
Ok(utf8) => utf8,
97-
Err(_) => String::new(),
98-
}
95+
String::from_utf8(str_slice.to_vec()).unwrap_or_default()
9996
}
10097

10198
// Location is the file defining the method, colon, method name.

zjit/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ expect-test = "1.5.1"
1919
# Support --yjit-dump-disasm and RubyVM::YJIT.disasm using libcapstone.
2020
disasm = ["capstone"]
2121
runtime_checks = []
22+
23+
[lints]
24+
workspace = true

zjit/src/backend/arm64/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ fn emit_load_value(cb: &mut CodeBlock, rd: A64Opnd, value: u64) -> usize {
139139
// If the value fits into a single movz
140140
// instruction, then we'll use that.
141141
movz(cb, rd, A64Opnd::new_uimm(current), 0);
142-
return 1;
142+
1
143143
} else if u16::try_from(!value).is_ok() {
144144
// For small negative values, use a single movn
145145
movn(cb, rd, A64Opnd::new_uimm(!value), 0);
@@ -182,7 +182,7 @@ fn emit_load_value(cb: &mut CodeBlock, rd: A64Opnd, value: u64) -> usize {
182182
/// List of registers that can be used for register allocation.
183183
/// This has the same number of registers for x86_64 and arm64.
184184
/// SCRATCH0 and SCRATCH1 are excluded.
185-
pub const ALLOC_REGS: &'static [Reg] = &[
185+
pub const ALLOC_REGS: &[Reg] = &[
186186
X0_REG,
187187
X1_REG,
188188
X2_REG,
@@ -480,7 +480,7 @@ impl Assembler
480480
// which is both the return value and first argument register
481481
if !opnds.is_empty() {
482482
let mut args: Vec<(Reg, Opnd)> = vec![];
483-
for (idx, opnd) in opnds.into_iter().enumerate().rev() {
483+
for (idx, opnd) in opnds.iter_mut().enumerate().rev() {
484484
// If the value that we're sending is 0, then we can use
485485
// the zero register, so in this case we'll just send
486486
// a UImm of 0 along as the argument to the move.
@@ -1106,8 +1106,8 @@ impl Assembler
11061106
// be stored is first and the address is second. However in
11071107
// our IR we have the address first and the register second.
11081108
match dest_num_bits {
1109-
64 | 32 => stur(cb, src.into(), dest.into()),
1110-
16 => sturh(cb, src.into(), dest.into()),
1109+
64 | 32 => stur(cb, src, dest),
1110+
16 => sturh(cb, src, dest),
11111111
num_bits => panic!("unexpected dest num_bits: {} (src: {:#?}, dest: {:#?})", num_bits, src, dest),
11121112
}
11131113
},
@@ -1411,7 +1411,7 @@ impl Assembler
14111411
///
14121412
/// If a, b, and c are all registers.
14131413
fn merge_three_reg_mov(
1414-
live_ranges: &Vec<LiveRange>,
1414+
live_ranges: &[LiveRange],
14151415
iterator: &mut std::iter::Peekable<impl Iterator<Item = (usize, Insn)>>,
14161416
left: &Opnd,
14171417
right: &Opnd,

0 commit comments

Comments
 (0)