Skip to content

Commit 8ba9541

Browse files
committed
Use workspace to set clippy linters
use them in yjit and zjit fix clippy issues
1 parent 07878eb commit 8ba9541

File tree

32 files changed

+247
-255
lines changed

32 files changed

+247
-255
lines changed

Cargo.toml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,34 @@ 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+
restriction = { level = "allow", priority = -1 }
63+
64+
too_many_arguments = "allow"
65+
identity_op = "allow"
66+
67+
ptr_arg = "forbid"
68+
ref_option = "forbid"
69+
inefficient_to_string = "forbid"
70+
from_over_into = "forbid"
71+
unwrap_or_default = "forbid"
72+
redundant_static_lifetimes = "forbid"
73+
match_like_matches_macro = "forbid"
74+
field_reassign_with_default = "forbid"
75+
inherent_to_string = "forbid"
76+
owned_cow = "forbid"
77+
redundant_clone = "forbid"
78+
str_to_string = "forbid"
79+
single_char_pattern = "forbid"
80+
single_char_add_str = "forbid"
81+
unnecessary_owned_empty_strings = "forbid"
82+
manual_string_new = "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/arm64/opnd.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,7 @@ impl A64Opnd {
7979

8080
/// Convenience function to check if this operand is a register.
8181
pub fn is_reg(&self) -> bool {
82-
match self {
83-
A64Opnd::Reg(_) => true,
84-
_ => false
85-
}
82+
matches!(self, A64Opnd::Reg(_))
8683
}
8784

8885
/// Unwrap a register from an operand.

yjit/src/asm/mod.rs

Lines changed: 9 additions & 9 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
@@ -378,7 +378,7 @@ impl CodeBlock {
378378

379379
// Unless this comment is the same as the last one at this same line, add it.
380380
if this_line_comments.last().map(String::as_str) != Some(comment) {
381-
this_line_comments.push(comment.to_string());
381+
this_line_comments.push(comment.into());
382382
}
383383
}
384384

@@ -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/asm/x86_64/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,7 @@ impl X86Opnd {
163163
}
164164

165165
pub fn is_some(&self) -> bool {
166-
match self {
167-
X86Opnd::None => false,
168-
_ => true
169-
}
166+
!matches!(self, X86Opnd::None)
170167
}
171168

172169
}

yjit/src/asm/x86_64/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ fn block_comments() {
454454
let third_write_ptr = cb.get_write_ptr().raw_addr(&cb);
455455
cb.add_comment("Ten bytes in");
456456

457-
assert_eq!(&vec!( "Beginning".to_string() ), cb.comments_at(first_write_ptr).unwrap());
458-
assert_eq!(&vec!( "Two bytes in".to_string(), "Still two bytes in".to_string() ), cb.comments_at(second_write_ptr).unwrap());
459-
assert_eq!(&vec!( "Ten bytes in".to_string() ), cb.comments_at(third_write_ptr).unwrap());
457+
assert_eq!(&vec!( "Beginning".to_owned() ), cb.comments_at(first_write_ptr).unwrap());
458+
assert_eq!(&vec!( "Two bytes in".to_owned(), "Still two bytes in".to_owned() ), cb.comments_at(second_write_ptr).unwrap());
459+
assert_eq!(&vec!( "Ten bytes in".to_owned() ), cb.comments_at(third_write_ptr).unwrap());
460460
}

yjit/src/backend/arm64/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,7 @@ impl Assembler
12431243
emit_cmp_zero_jump(cb, opnd.into(), false, compile_side_exit(*target, self, ocb)?);
12441244
},
12451245
Insn::IncrCounter { mem, value } => {
1246-
let label = cb.new_label("incr_counter_loop".to_string());
1246+
let label = cb.new_label("incr_counter_loop".into());
12471247
cb.write_label(label);
12481248

12491249
ldaxr(cb, Self::SCRATCH0, mem.into());

yjit/src/backend/ir.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ impl Opnd
186186

187187
/// Maps the indices from a previous list of instructions to a new list of
188188
/// instructions.
189-
pub fn map_index(self, indices: &Vec<usize>) -> Opnd {
189+
pub fn map_index(self, indices: &[usize]) -> Opnd {
190190
match self {
191191
Opnd::InsnOut { idx, num_bits } => {
192192
Opnd::InsnOut { idx: indices[idx], num_bits }
@@ -1186,7 +1186,7 @@ impl Assembler
11861186
assert!(!name.contains(' '), "use underscores in label names, not spaces");
11871187

11881188
let label_idx = self.label_names.len();
1189-
self.label_names.push(name.to_string());
1189+
self.label_names.push(name.into());
11901190
Target::Label(label_idx)
11911191
}
11921192

@@ -1266,11 +1266,11 @@ impl Assembler
12661266

12671267
/// Spill all live registers to the stack
12681268
pub fn spill_regs(&mut self) {
1269-
self.spill_regs_except(&vec![]);
1269+
self.spill_regs_except(&[]);
12701270
}
12711271

12721272
/// Spill all live registers except `ignored_temps` to the stack
1273-
pub fn spill_regs_except(&mut self, ignored_temps: &Vec<RegOpnd>) {
1273+
pub fn spill_regs_except(&mut self, ignored_temps: &[RegOpnd]) {
12741274
// Forget registers above the stack top
12751275
let mut reg_mapping = self.ctx.get_reg_mapping();
12761276
for stack_idx in self.ctx.get_stack_size()..MAX_CTX_TEMPS as u8 {
@@ -1348,17 +1348,17 @@ impl Assembler
13481348

13491349
// Shuffle register moves, sometimes adding extra moves using SCRATCH_REG,
13501350
// so that they will not rewrite each other before they are used.
1351-
pub fn reorder_reg_moves(old_moves: &Vec<(Reg, Opnd)>) -> Vec<(Reg, Opnd)> {
1351+
pub fn reorder_reg_moves(old_moves: &[(Reg, Opnd)]) -> Vec<(Reg, Opnd)> {
13521352
// Return the index of a move whose destination is not used as a source if any.
1353-
fn find_safe_move(moves: &Vec<(Reg, Opnd)>) -> Option<usize> {
1353+
fn find_safe_move(moves: &[(Reg, Opnd)]) -> Option<usize> {
13541354
moves.iter().enumerate().find(|(_, &(dest_reg, _))| {
13551355
moves.iter().all(|&(_, src_opnd)| src_opnd != Opnd::Reg(dest_reg))
13561356
}).map(|(index, _)| index)
13571357
}
13581358

13591359
// Remove moves whose source and destination are the same
1360-
let mut old_moves: Vec<(Reg, Opnd)> = old_moves.clone().into_iter()
1361-
.filter(|&(reg, opnd)| Opnd::Reg(reg) != opnd).collect();
1360+
let mut old_moves: Vec<(Reg, Opnd)> = old_moves.into_iter()
1361+
.filter(|&&(reg, opnd)| Opnd::Reg(reg) != opnd).copied().collect();
13621362

13631363
let mut new_moves = vec![];
13641364
while old_moves.len() > 0 {
@@ -1385,6 +1385,7 @@ impl Assembler
13851385
/// Sets the out field on the various instructions that require allocated
13861386
/// registers because their output is used as the operand on a subsequent
13871387
/// instruction. This is our implementation of the linear scan algorithm.
1388+
#[allow(clippy::useless_conversion)]
13881389
pub(super) fn alloc_regs(mut self, regs: Vec<Reg>) -> Assembler
13891390
{
13901391
//dbg!(&self);
@@ -1394,7 +1395,7 @@ impl Assembler
13941395

13951396
// Mutate the pool bitmap to indicate that the register at that index
13961397
// has been allocated and is live.
1397-
fn alloc_reg(pool: &mut u32, regs: &Vec<Reg>) -> Option<Reg> {
1398+
fn alloc_reg(pool: &mut u32, regs: &[Reg]) -> Option<Reg> {
13981399
for (index, reg) in regs.iter().enumerate() {
13991400
if (*pool & (1 << index)) == 0 {
14001401
*pool |= 1 << index;
@@ -1405,7 +1406,7 @@ impl Assembler
14051406
}
14061407

14071408
// Allocate a specific register
1408-
fn take_reg(pool: &mut u32, regs: &Vec<Reg>, reg: &Reg) -> Reg {
1409+
fn take_reg(pool: &mut u32, regs: &[Reg], reg: &Reg) -> Reg {
14091410
let reg_index = regs.iter().position(|elem| elem.reg_no == reg.reg_no);
14101411

14111412
if let Some(reg_index) = reg_index {
@@ -1419,7 +1420,7 @@ impl Assembler
14191420
// Mutate the pool bitmap to indicate that the given register is being
14201421
// returned as it is no longer used by the instruction that previously
14211422
// held it.
1422-
fn dealloc_reg(pool: &mut u32, regs: &Vec<Reg>, reg: &Reg) {
1423+
fn dealloc_reg(pool: &mut u32, regs: &[Reg], reg: &Reg) {
14231424
let reg_index = regs.iter().position(|elem| elem.reg_no == reg.reg_no);
14241425

14251426
if let Some(reg_index) = reg_index {
@@ -1602,7 +1603,7 @@ impl Assembler
16021603
if c_args.len() > 0 {
16031604
// Resolve C argument dependencies
16041605
let c_args_len = c_args.len() as isize;
1605-
let moves = Self::reorder_reg_moves(&c_args.drain(..).into_iter().collect());
1606+
let moves = Self::reorder_reg_moves(&c_args.drain(..).into_iter().collect::<Vec<_>>());
16061607
shift_live_ranges(&mut shifted_live_ranges, asm.insns.len(), moves.len() as isize - c_args_len);
16071608

16081609
// Push batched C arguments
@@ -1751,7 +1752,7 @@ impl Assembler {
17511752
}
17521753

17531754
pub fn bake_string(&mut self, text: &str) {
1754-
self.push_insn(Insn::BakeString(text.to_string()));
1755+
self.push_insn(Insn::BakeString(text.into()));
17551756
}
17561757

17571758
#[allow(dead_code)]
@@ -1791,7 +1792,7 @@ impl Assembler {
17911792
}
17921793

17931794
/// Let vm_check_canary() assert the leafness of this ccall if leaf_ccall is set
1794-
fn set_stack_canary(&mut self, opnds: &Vec<Opnd>) -> Option<Opnd> {
1795+
fn set_stack_canary(&mut self, opnds: &[Opnd]) -> Option<Opnd> {
17951796
// Use the slot right above the stack top for verifying leafness.
17961797
let canary_opnd = self.stack_opnd(-1);
17971798

yjit/src/codegen.rs

Lines changed: 11 additions & 13 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,
@@ -403,7 +404,7 @@ impl<'a> JITState<'a> {
403404
if !self.perf_stack.is_empty() {
404405
self.perf_symbol_range_end(asm);
405406
}
406-
self.perf_stack.push(symbol_name.to_string());
407+
self.perf_stack.push(symbol_name.into());
407408
self.perf_symbol_range_start(asm, symbol_name);
408409
}
409410

@@ -453,12 +454,9 @@ impl<'a> JITState<'a> {
453454

454455
/// Return true if we're compiling a send-like instruction, not an opt_* instruction.
455456
pub fn is_sendish(&self) -> bool {
456-
match unsafe { rb_iseq_opcode_at_pc(self.iseq, self.pc) } as u32 {
457-
YARVINSN_send |
457+
matches!(unsafe { rb_iseq_opcode_at_pc(self.iseq, self.pc) } as u32, YARVINSN_send |
458458
YARVINSN_opt_send_without_block |
459-
YARVINSN_invokesuper => true,
460-
_ => false,
461-
}
459+
YARVINSN_invokesuper)
462460
}
463461

464462
/// Return the number of locals in the current ISEQ
@@ -1178,14 +1176,14 @@ pub fn gen_entry_reg_mapping(asm: &mut Assembler, blockid: BlockId, stack_size:
11781176
// Find an existing callee block. If it's not found or uses no register, skip loading registers.
11791177
let mut ctx = Context::default();
11801178
ctx.set_stack_size(stack_size);
1181-
let reg_mapping = find_most_compatible_reg_mapping(blockid, &ctx).unwrap_or(RegMapping::default());
1179+
let reg_mapping = find_most_compatible_reg_mapping(blockid, &ctx).unwrap_or_default();
11821180
if reg_mapping == RegMapping::default() {
11831181
return reg_mapping;
11841182
}
11851183

11861184
// If found, load the same registers to reuse the block.
11871185
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();
1186+
let local_table_size: u32 = unsafe { get_iseq_body_local_table_size(blockid.iseq) };
11891187
for &reg_opnd in reg_mapping.get_reg_opnds().iter() {
11901188
match reg_opnd {
11911189
RegOpnd::Local(local_idx) => {
@@ -1299,7 +1297,7 @@ pub fn gen_single_block(
12991297
#[cfg(feature = "disasm")]
13001298
if get_option_ref!(dump_disasm).is_some() {
13011299
let blockid_idx = blockid.idx;
1302-
let chain_depth = if asm.ctx.get_chain_depth() > 0 { format!("(chain_depth: {})", asm.ctx.get_chain_depth()) } else { "".to_string() };
1300+
let chain_depth = if asm.ctx.get_chain_depth() > 0 { format!("(chain_depth: {})", asm.ctx.get_chain_depth()) } else { String::new() };
13031301
asm_comment!(asm, "Block: {} {}", iseq_get_location(blockid.iseq, blockid_idx), chain_depth);
13041302
asm_comment!(asm, "reg_mapping: {:?}", asm.ctx.get_reg_mapping());
13051303
}
@@ -7708,7 +7706,7 @@ fn gen_send_iseq(
77087706
// runtime guards later in copy_splat_args_for_rest_callee()
77097707
if !iseq_has_rest {
77107708
let supplying = argc - 1 - i32::from(kw_splat) + array_length as i32;
7711-
if (required_num..=required_num + opt_num).contains(&supplying) == false {
7709+
if !(required_num..=required_num + opt_num).contains(&supplying) {
77127710
gen_counter_incr(jit, asm, Counter::send_iseq_splat_arity_error);
77137711
return None;
77147712
}
@@ -9546,7 +9544,7 @@ fn get_class_name(class: Option<VALUE>) -> String {
95469544
unsafe { RB_TYPE_P(class, RUBY_T_MODULE) || RB_TYPE_P(class, RUBY_T_CLASS) }
95479545
}).and_then(|class| unsafe {
95489546
cstr_to_rust_string(rb_class2name(class))
9549-
}).unwrap_or_else(|| "Unknown".to_string())
9547+
}).unwrap_or_else(|| "Unknown".into())
95509548
}
95519549

95529550
/// Assemble "{class_name}#{method_name}" from a class pointer and a method ID
@@ -9556,15 +9554,15 @@ fn get_method_name(class: Option<VALUE>, mid: u64) -> String {
95569554
unsafe { cstr_to_rust_string(rb_id2name(mid)) }
95579555
} else {
95589556
None
9559-
}.unwrap_or_else(|| "Unknown".to_string());
9557+
}.unwrap_or_else(|| "Unknown".into());
95609558
format!("{}#{}", class_name, method_name)
95619559
}
95629560

95639561
/// Assemble "{label}@{iseq_path}:{lineno}" (iseq_inspect() format) from an ISEQ
95649562
fn get_iseq_name(iseq: IseqPtr) -> String {
95659563
let c_string = unsafe { rb_yjit_iseq_inspect(iseq) };
95669564
let string = unsafe { CStr::from_ptr(c_string) }.to_str()
9567-
.unwrap_or_else(|_| "not UTF-8").to_string();
9565+
.unwrap_or_else(|_| "not UTF-8").into();
95689566
unsafe { ruby_xfree(c_string as *mut c_void); }
95699567
string
95709568
}

0 commit comments

Comments
 (0)