Skip to content

Commit a6d397e

Browse files
authored
ZJIT: Ensure clippy passes and silence unnecessary warnings (ruby#14439)
1 parent 8a35077 commit a6d397e

File tree

25 files changed

+183
-160
lines changed

25 files changed

+183
-160
lines changed

.github/workflows/zjit-ubuntu.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,25 @@ permissions:
2727
contents: read
2828

2929
jobs:
30+
lint:
31+
name: cargo clippy
32+
33+
runs-on: ubuntu-22.04
34+
35+
if: >-
36+
${{!(false
37+
|| contains(github.event.head_commit.message, '[DOC]')
38+
|| contains(github.event.pull_request.title, '[DOC]')
39+
|| contains(github.event.pull_request.labels.*.name, 'Documentation')
40+
|| (github.event_name == 'push' && github.event.pull_request.user.login == 'dependabot[bot]')
41+
)}}
42+
43+
steps:
44+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
45+
46+
- run: cargo clippy --all-targets --all-features
47+
working-directory: zjit
48+
3049
make:
3150
strategy:
3251
fail-fast: false

zjit/src/asm/arm64/inst/branch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ mod tests {
8989
#[test]
9090
fn test_ret() {
9191
let result: u32 = Branch::ret(30).into();
92-
assert_eq!(0xd65f03C0, result);
92+
assert_eq!(0xd65f03c0, result);
9393
}
9494

9595
#[test]

zjit/src/asm/arm64/inst/load_literal.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(clippy::identity_op)]
2+
13
use super::super::arg::{InstructionOffset, truncate_imm};
24

35
/// The size of the operands being operated on.

zjit/src/asm/arm64/inst/mov.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,14 @@ mod tests {
145145
fn test_movk_shifted_16() {
146146
let inst = Mov::movk(0, 123, 16, 64);
147147
let result: u32 = inst.into();
148-
assert_eq!(0xf2A00f60, result);
148+
assert_eq!(0xf2a00f60, result);
149149
}
150150

151151
#[test]
152152
fn test_movk_shifted_32() {
153153
let inst = Mov::movk(0, 123, 32, 64);
154154
let result: u32 = inst.into();
155-
assert_eq!(0xf2C00f60, result);
155+
assert_eq!(0xf2c00f60, result);
156156
}
157157

158158
#[test]

zjit/src/asm/arm64/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#![allow(dead_code)] // For instructions and operands we're not currently using.
2+
#![allow(clippy::upper_case_acronyms)]
3+
#![allow(clippy::identity_op)]
4+
#![allow(clippy::self_named_constructors)]
5+
#![allow(clippy::unusual_byte_groupings)]
26

37
use crate::asm::CodeBlock;
48

@@ -1590,7 +1594,7 @@ mod tests {
15901594

15911595
#[test]
15921596
fn test_nop() {
1593-
check_bytes("1f2003d5", |cb| nop(cb));
1597+
check_bytes("1f2003d5", nop);
15941598
}
15951599

15961600
#[test]

zjit/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.

zjit/src/asm/x86_64/mod.rs

Lines changed: 3 additions & 6 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
}
@@ -284,11 +281,11 @@ pub fn mem_opnd(num_bits: u8, base_reg: X86Opnd, disp: i32) -> X86Opnd
284281
} else {
285282
X86Opnd::Mem(
286283
X86Mem {
287-
num_bits: num_bits,
284+
num_bits,
288285
base_reg_no: base_reg.reg_no,
289286
idx_reg_no: None,
290287
scale_exp: 0,
291-
disp: disp,
288+
disp,
292289
}
293290
)
294291
}

zjit/src/asm/x86_64/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn test_add() {
3333
fn test_add_unsigned() {
3434
// ADD r/m8, imm8
3535
check_bytes("4180c001", |cb| add(cb, R8B, uimm_opnd(1)));
36-
check_bytes("4180c07f", |cb| add(cb, R8B, imm_opnd(i8::MAX.try_into().unwrap())));
36+
check_bytes("4180c07f", |cb| add(cb, R8B, imm_opnd(i8::MAX.into())));
3737

3838
// ADD r/m16, imm16
3939
check_bytes("664183c001", |cb| add(cb, R8W, uimm_opnd(1)));
@@ -102,7 +102,7 @@ fn test_cmp() {
102102

103103
#[test]
104104
fn test_cqo() {
105-
check_bytes("4899", |cb| cqo(cb));
105+
check_bytes("4899", cqo);
106106
}
107107

108108
#[test]
@@ -341,7 +341,7 @@ fn test_push() {
341341

342342
#[test]
343343
fn test_ret() {
344-
check_bytes("c3", |cb| ret(cb));
344+
check_bytes("c3", ret);
345345
}
346346

347347
#[test]

zjit/src/backend/arm64/mod.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ fn emit_load_value(cb: &mut CodeBlock, rd: A64Opnd, value: u64) -> usize {
183183
/// List of registers that can be used for register allocation.
184184
/// This has the same number of registers for x86_64 and arm64.
185185
/// SCRATCH0 and SCRATCH1 are excluded.
186-
pub const ALLOC_REGS: &'static [Reg] = &[
186+
pub const ALLOC_REGS: &[Reg] = &[
187187
X0_REG,
188188
X1_REG,
189189
X2_REG,
@@ -386,15 +386,12 @@ impl Assembler
386386
let mut opnd_iter = insn.opnd_iter_mut();
387387

388388
while let Some(opnd) = opnd_iter.next() {
389-
match opnd {
390-
Opnd::Value(value) => {
391-
if value.special_const_p() {
392-
*opnd = Opnd::UImm(value.as_u64());
393-
} else if !is_load {
394-
*opnd = asm.load(*opnd);
395-
}
396-
},
397-
_ => {}
389+
if let Opnd::Value(value) = opnd {
390+
if value.special_const_p() {
391+
*opnd = Opnd::UImm(value.as_u64());
392+
} else if !is_load {
393+
*opnd = asm.load(*opnd);
394+
}
398395
};
399396
}
400397

@@ -481,7 +478,7 @@ impl Assembler
481478
// which is both the return value and first argument register
482479
if !opnds.is_empty() {
483480
let mut args: Vec<(Reg, Opnd)> = vec![];
484-
for (idx, opnd) in opnds.into_iter().enumerate().rev() {
481+
for (idx, opnd) in opnds.iter_mut().enumerate().rev() {
485482
// If the value that we're sending is 0, then we can use
486483
// the zero register, so in this case we'll just send
487484
// a UImm of 0 along as the argument to the move.
@@ -1411,7 +1408,7 @@ impl Assembler
14111408
///
14121409
/// If a, b, and c are all registers.
14131410
fn merge_three_reg_mov(
1414-
live_ranges: &Vec<LiveRange>,
1411+
live_ranges: &[LiveRange],
14151412
iterator: &mut std::iter::Peekable<impl Iterator<Item = (usize, Insn)>>,
14161413
left: &Opnd,
14171414
right: &Opnd,
@@ -1566,7 +1563,7 @@ mod tests {
15661563

15671564
#[test]
15681565
fn frame_setup_and_teardown() {
1569-
const THREE_REGS: &'static [Opnd] = &[Opnd::Reg(X19_REG), Opnd::Reg(X20_REG), Opnd::Reg(X21_REG)];
1566+
const THREE_REGS: &[Opnd] = &[Opnd::Reg(X19_REG), Opnd::Reg(X20_REG), Opnd::Reg(X21_REG)];
15701567
// Test 3 preserved regs (odd), odd slot_count
15711568
{
15721569
let (mut asm, mut cb) = setup_asm();
@@ -1607,7 +1604,7 @@ mod tests {
16071604

16081605
// Test 4 preserved regs (even), odd slot_count
16091606
{
1610-
static FOUR_REGS: &'static [Opnd] = &[Opnd::Reg(X19_REG), Opnd::Reg(X20_REG), Opnd::Reg(X21_REG), Opnd::Reg(X22_REG)];
1607+
static FOUR_REGS: &[Opnd] = &[Opnd::Reg(X19_REG), Opnd::Reg(X20_REG), Opnd::Reg(X21_REG), Opnd::Reg(X22_REG)];
16111608
let (mut asm, mut cb) = setup_asm();
16121609
asm.frame_setup(FOUR_REGS, 3);
16131610
asm.frame_teardown(FOUR_REGS);

zjit/src/backend/lir.rs

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub use crate::backend::current::{
1818
};
1919
pub const SCRATCH_OPND: Opnd = Opnd::Reg(Assembler::SCRATCH_REG);
2020

21-
pub static JIT_PRESERVED_REGS: &'static [Opnd] = &[CFP, SP, EC];
21+
pub static JIT_PRESERVED_REGS: &[Opnd] = &[CFP, SP, EC];
2222

2323
// Memory operand base
2424
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
@@ -99,17 +99,17 @@ impl Opnd
9999
assert!(base_reg.num_bits == 64);
100100
Opnd::Mem(Mem {
101101
base: MemBase::Reg(base_reg.reg_no),
102-
disp: disp,
103-
num_bits: num_bits,
102+
disp,
103+
num_bits,
104104
})
105105
},
106106

107107
Opnd::VReg{idx, num_bits: out_num_bits } => {
108108
assert!(num_bits <= out_num_bits);
109109
Opnd::Mem(Mem {
110110
base: MemBase::VReg(idx),
111-
disp: disp,
112-
num_bits: num_bits,
111+
disp,
112+
num_bits,
113113
})
114114
},
115115

@@ -1215,8 +1215,8 @@ impl Assembler
12151215
}
12161216

12171217
// If we find any VReg from previous instructions, extend the live range to insn_idx
1218-
let mut opnd_iter = insn.opnd_iter();
1219-
while let Some(opnd) = opnd_iter.next() {
1218+
let opnd_iter = insn.opnd_iter();
1219+
for opnd in opnd_iter {
12201220
match *opnd {
12211221
Opnd::VReg { idx, .. } |
12221222
Opnd::Mem(Mem { base: MemBase::VReg(idx), .. }) => {
@@ -1243,16 +1243,16 @@ impl Assembler
12431243

12441244
// Shuffle register moves, sometimes adding extra moves using SCRATCH_REG,
12451245
// so that they will not rewrite each other before they are used.
1246-
pub fn resolve_parallel_moves(old_moves: &Vec<(Reg, Opnd)>) -> Vec<(Reg, Opnd)> {
1246+
pub fn resolve_parallel_moves(old_moves: &[(Reg, Opnd)]) -> Vec<(Reg, Opnd)> {
12471247
// Return the index of a move whose destination is not used as a source if any.
1248-
fn find_safe_move(moves: &Vec<(Reg, Opnd)>) -> Option<usize> {
1248+
fn find_safe_move(moves: &[(Reg, Opnd)]) -> Option<usize> {
12491249
moves.iter().enumerate().find(|&(_, &(dest_reg, _))| {
12501250
moves.iter().all(|&(_, src_opnd)| src_opnd != Opnd::Reg(dest_reg))
12511251
}).map(|(index, _)| index)
12521252
}
12531253

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

12581258
let mut new_moves = vec![];
@@ -1386,19 +1386,19 @@ impl Assembler
13861386
Some(Opnd::VReg { idx, .. }) => Some(*idx),
13871387
_ => None,
13881388
};
1389-
if vreg_idx.is_some() {
1390-
if live_ranges[vreg_idx.unwrap()].end() == index {
1391-
debug!("Allocating a register for VReg({}) at instruction index {} even though it does not live past this index", vreg_idx.unwrap(), index);
1389+
if let Some(vreg_idx) = vreg_idx {
1390+
if live_ranges[vreg_idx].end() == index {
1391+
debug!("Allocating a register for VReg({}) at instruction index {} even though it does not live past this index", vreg_idx, index);
13921392
}
13931393
// This is going to be the output operand that we will set on the
13941394
// instruction. CCall and LiveReg need to use a specific register.
13951395
let mut out_reg = match insn {
13961396
Insn::CCall { .. } => {
1397-
Some(pool.take_reg(&C_RET_REG, vreg_idx.unwrap()))
1397+
Some(pool.take_reg(&C_RET_REG, vreg_idx))
13981398
}
13991399
Insn::LiveReg { opnd, .. } => {
14001400
let reg = opnd.unwrap_reg();
1401-
Some(pool.take_reg(&reg, vreg_idx.unwrap()))
1401+
Some(pool.take_reg(&reg, vreg_idx))
14021402
}
14031403
_ => None
14041404
};
@@ -1414,7 +1414,7 @@ impl Assembler
14141414
if let Some(Opnd::VReg{ idx, .. }) = opnd_iter.next() {
14151415
if live_ranges[*idx].end() == index {
14161416
if let Some(reg) = reg_mapping[*idx] {
1417-
out_reg = Some(pool.take_reg(&reg, vreg_idx.unwrap()));
1417+
out_reg = Some(pool.take_reg(&reg, vreg_idx));
14181418
}
14191419
}
14201420
}
@@ -1423,21 +1423,19 @@ impl Assembler
14231423
// Allocate a new register for this instruction if one is not
14241424
// already allocated.
14251425
if out_reg.is_none() {
1426-
out_reg = match &insn {
1427-
_ => match pool.alloc_reg(vreg_idx.unwrap()) {
1428-
Some(reg) => Some(reg),
1429-
None => {
1430-
if get_option!(debug) {
1431-
let mut insns = asm.insns;
1426+
out_reg = match pool.alloc_reg(vreg_idx) {
1427+
Some(reg) => Some(reg),
1428+
None => {
1429+
if get_option!(debug) {
1430+
let mut insns = asm.insns;
1431+
insns.push(insn);
1432+
for (_, insn) in iterator.by_ref() {
14321433
insns.push(insn);
1433-
while let Some((_, insn)) = iterator.next() {
1434-
insns.push(insn);
1435-
}
1436-
dump_live_regs(insns, live_ranges, regs.len(), index);
14371434
}
1438-
debug!("Register spill not supported");
1439-
return Err(CompileError::RegisterSpillOnAlloc);
1435+
dump_live_regs(insns, live_ranges, regs.len(), index);
14401436
}
1437+
debug!("Register spill not supported");
1438+
return Err(CompileError::RegisterSpillOnAlloc);
14411439
}
14421440
};
14431441
}
@@ -1510,7 +1508,7 @@ impl Assembler
15101508
if is_ccall {
15111509
// On x86_64, maintain 16-byte stack alignment
15121510
if cfg!(target_arch = "x86_64") && saved_regs.len() % 2 == 1 {
1513-
asm.cpop_into(Opnd::Reg(saved_regs.last().unwrap().0.clone()));
1511+
asm.cpop_into(Opnd::Reg(saved_regs.last().unwrap().0));
15141512
}
15151513
// Restore saved registers
15161514
for &(reg, vreg_idx) in saved_regs.iter().rev() {
@@ -1527,7 +1525,6 @@ impl Assembler
15271525

15281526
/// Compile the instructions down to machine code.
15291527
/// Can fail due to lack of code memory and inopportune code placement, among other reasons.
1530-
#[must_use]
15311528
pub fn compile(self, cb: &mut CodeBlock) -> Result<(CodePtr, Vec<CodePtr>), CompileError> {
15321529
#[cfg(feature = "disasm")]
15331530
let start_addr = cb.get_write_ptr();
@@ -2038,7 +2035,7 @@ mod tests {
20382035
assert!(matches!(opnd_iter.next(), Some(Opnd::None)));
20392036
assert!(matches!(opnd_iter.next(), Some(Opnd::None)));
20402037

2041-
assert!(matches!(opnd_iter.next(), None));
2038+
assert!(opnd_iter.next().is_none());
20422039
}
20432040

20442041
#[test]
@@ -2049,7 +2046,7 @@ mod tests {
20492046
assert!(matches!(opnd_iter.next(), Some(Opnd::None)));
20502047
assert!(matches!(opnd_iter.next(), Some(Opnd::None)));
20512048

2052-
assert!(matches!(opnd_iter.next(), None));
2049+
assert!(opnd_iter.next().is_none());
20532050
}
20542051

20552052
#[test]

0 commit comments

Comments
 (0)