Skip to content

Commit ed4b477

Browse files
committed
clippy: Fix Clippy reports
Actually, let Clippy fix most of these itself. I only removed the unused ErrorKind import from tests/misc.rs. Signed-off-by: Quentin Monnet <qmo@qmon.net>
1 parent de92a50 commit ed4b477

File tree

8 files changed

+50
-60
lines changed

8 files changed

+50
-60
lines changed

src/disassembler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ pub struct HLInsn {
196196
/// ]);
197197
/// ```
198198
pub fn to_insn_vec(prog: &[u8]) -> Vec<HLInsn> {
199-
if prog.len() % ebpf::INSN_SIZE != 0 {
199+
if !prog.len().is_multiple_of(ebpf::INSN_SIZE) {
200200
panic!(
201201
"[Disassembler] Error: eBPF program length must be a multiple of {:?} octets",
202202
ebpf::INSN_SIZE

src/ebpf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ pub fn get_insn(prog: &[u8], idx: usize) -> Insn {
616616
/// ]);
617617
/// ```
618618
pub fn to_insn_vec(prog: &[u8]) -> Vec<Insn> {
619-
if prog.len() % INSN_SIZE != 0 {
619+
if !prog.len().is_multiple_of(INSN_SIZE) {
620620
panic!(
621621
"Error: eBPF program length must be a multiple of {INSN_SIZE:?} octets"
622622
);

src/insn_builder.rs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -110,72 +110,72 @@ impl BpfCode {
110110
}
111111

112112
/// create ADD instruction
113-
pub fn add(&mut self, source: Source, arch: Arch) -> Move {
113+
pub fn add(&mut self, source: Source, arch: Arch) -> Move<'_> {
114114
self.mov_internal(source, arch, OpBits::Add)
115115
}
116116

117117
/// create SUB instruction
118-
pub fn sub(&mut self, source: Source, arch: Arch) -> Move {
118+
pub fn sub(&mut self, source: Source, arch: Arch) -> Move<'_> {
119119
self.mov_internal(source, arch, OpBits::Sub)
120120
}
121121

122122
/// create MUL instruction
123-
pub fn mul(&mut self, source: Source, arch: Arch) -> Move {
123+
pub fn mul(&mut self, source: Source, arch: Arch) -> Move<'_> {
124124
self.mov_internal(source, arch, OpBits::Mul)
125125
}
126126

127127
/// create DIV instruction
128-
pub fn div(&mut self, source: Source, arch: Arch) -> Move {
128+
pub fn div(&mut self, source: Source, arch: Arch) -> Move<'_> {
129129
self.mov_internal(source, arch, OpBits::Div)
130130
}
131131

132132
/// create OR instruction
133-
pub fn bit_or(&mut self, source: Source, arch: Arch) -> Move {
133+
pub fn bit_or(&mut self, source: Source, arch: Arch) -> Move<'_> {
134134
self.mov_internal(source, arch, OpBits::BitOr)
135135
}
136136

137137
/// create AND instruction
138-
pub fn bit_and(&mut self, source: Source, arch: Arch) -> Move {
138+
pub fn bit_and(&mut self, source: Source, arch: Arch) -> Move<'_> {
139139
self.mov_internal(source, arch, OpBits::BitAnd)
140140
}
141141

142142
/// create LSHIFT instruction
143-
pub fn left_shift(&mut self, source: Source, arch: Arch) -> Move {
143+
pub fn left_shift(&mut self, source: Source, arch: Arch) -> Move<'_> {
144144
self.mov_internal(source, arch, OpBits::LShift)
145145
}
146146

147147
/// create RSHIFT instruction
148-
pub fn right_shift(&mut self, source: Source, arch: Arch) -> Move {
148+
pub fn right_shift(&mut self, source: Source, arch: Arch) -> Move<'_> {
149149
self.mov_internal(source, arch, OpBits::RShift)
150150
}
151151

152152
/// create NEGATE instruction
153-
pub fn negate(&mut self, arch: Arch) -> Move {
153+
pub fn negate(&mut self, arch: Arch) -> Move<'_> {
154154
self.mov_internal(Source::Imm, arch, OpBits::Negate)
155155
}
156156

157157
/// create MOD instruction
158-
pub fn modulo(&mut self, source: Source, arch: Arch) -> Move {
158+
pub fn modulo(&mut self, source: Source, arch: Arch) -> Move<'_> {
159159
self.mov_internal(source, arch, OpBits::Mod)
160160
}
161161

162162
/// create XOR instruction
163-
pub fn bit_xor(&mut self, source: Source, arch: Arch) -> Move {
163+
pub fn bit_xor(&mut self, source: Source, arch: Arch) -> Move<'_> {
164164
self.mov_internal(source, arch, OpBits::BitXor)
165165
}
166166

167167
/// create MOV instruction
168-
pub fn mov(&mut self, source: Source, arch: Arch) -> Move {
168+
pub fn mov(&mut self, source: Source, arch: Arch) -> Move<'_> {
169169
self.mov_internal(source, arch, OpBits::Mov)
170170
}
171171

172172
/// create SIGNED RSHIFT instruction
173-
pub fn signed_right_shift(&mut self, source: Source, arch: Arch) -> Move {
173+
pub fn signed_right_shift(&mut self, source: Source, arch: Arch) -> Move<'_> {
174174
self.mov_internal(source, arch, OpBits::SignRShift)
175175
}
176176

177177
#[inline]
178-
fn mov_internal(&mut self, source: Source, arch_bits: Arch, op_bits: OpBits) -> Move {
178+
fn mov_internal(&mut self, source: Source, arch_bits: Arch, op_bits: OpBits) -> Move<'_> {
179179
Move {
180180
bpf_code: self,
181181
src_bit: source,
@@ -192,7 +192,7 @@ impl BpfCode {
192192
}
193193

194194
/// create byte swap instruction
195-
pub fn swap_bytes(&mut self, endian: Endian) -> SwapBytes {
195+
pub fn swap_bytes(&mut self, endian: Endian) -> SwapBytes<'_> {
196196
SwapBytes {
197197
bpf_code: self,
198198
endian,
@@ -207,27 +207,27 @@ impl BpfCode {
207207
}
208208

209209
/// create LOAD instruction, IMMEDIATE is the source
210-
pub fn load(&mut self, mem_size: MemSize) -> Load {
210+
pub fn load(&mut self, mem_size: MemSize) -> Load<'_> {
211211
self.load_internal(mem_size, Addressing::Imm, BPF_LD)
212212
}
213213

214214
/// create ABSOLUTE LOAD instruction
215-
pub fn load_abs(&mut self, mem_size: MemSize) -> Load {
215+
pub fn load_abs(&mut self, mem_size: MemSize) -> Load<'_> {
216216
self.load_internal(mem_size, Addressing::Abs, BPF_LD)
217217
}
218218

219219
/// create INDIRECT LOAD instruction
220-
pub fn load_ind(&mut self, mem_size: MemSize) -> Load {
220+
pub fn load_ind(&mut self, mem_size: MemSize) -> Load<'_> {
221221
self.load_internal(mem_size, Addressing::Ind, BPF_LD)
222222
}
223223

224224
/// create LOAD instruction, MEMORY is the source
225-
pub fn load_x(&mut self, mem_size: MemSize) -> Load {
225+
pub fn load_x(&mut self, mem_size: MemSize) -> Load<'_> {
226226
self.load_internal(mem_size, Addressing::Mem, BPF_LDX)
227227
}
228228

229229
#[inline]
230-
fn load_internal(&mut self, mem_size: MemSize, addressing: Addressing, source: u8) -> Load {
230+
fn load_internal(&mut self, mem_size: MemSize, addressing: Addressing, source: u8) -> Load<'_> {
231231
Load {
232232
bpf_code: self,
233233
addressing,
@@ -244,17 +244,17 @@ impl BpfCode {
244244
}
245245

246246
/// creates STORE instruction, IMMEDIATE is the source
247-
pub fn store(&mut self, mem_size: MemSize) -> Store {
247+
pub fn store(&mut self, mem_size: MemSize) -> Store<'_> {
248248
self.store_internal(mem_size, BPF_IMM)
249249
}
250250

251251
/// creates STORE instruction, MEMORY is the source
252-
pub fn store_x(&mut self, mem_size: MemSize) -> Store {
252+
pub fn store_x(&mut self, mem_size: MemSize) -> Store<'_> {
253253
self.store_internal(mem_size, BPF_MEM | BPF_STX)
254254
}
255255

256256
#[inline]
257-
fn store_internal(&mut self, mem_size: MemSize, source: u8) -> Store {
257+
fn store_internal(&mut self, mem_size: MemSize, source: u8) -> Store<'_> {
258258
Store {
259259
bpf_code: self,
260260
mem_size,
@@ -270,12 +270,12 @@ impl BpfCode {
270270
}
271271

272272
/// create unconditional JMP instruction
273-
pub fn jump_unconditional(&mut self) -> Jump {
273+
pub fn jump_unconditional(&mut self) -> Jump<'_> {
274274
self.jump_conditional(Cond::Abs, Source::Imm)
275275
}
276276

277277
/// create conditional JMP instruction
278-
pub fn jump_conditional(&mut self, cond: Cond, src_bit: Source) -> Jump {
278+
pub fn jump_conditional(&mut self, cond: Cond, src_bit: Source) -> Jump<'_> {
279279
Jump {
280280
bpf_code: self,
281281
cond,
@@ -291,7 +291,7 @@ impl BpfCode {
291291
}
292292

293293
/// create CALL instruction
294-
pub fn call(&mut self) -> FunctionCall {
294+
pub fn call(&mut self) -> FunctionCall<'_> {
295295
FunctionCall {
296296
bpf_code: self,
297297
insn: Insn {
@@ -305,7 +305,7 @@ impl BpfCode {
305305
}
306306

307307
/// create EXIT instruction
308-
pub fn exit(&mut self) -> Exit {
308+
pub fn exit(&mut self) -> Exit<'_> {
309309
Exit {
310310
bpf_code: self,
311311
insn: Insn {

src/interpreter.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn check_mem(
3737
}
3838
}
3939

40-
Err(Error::new(ErrorKind::Other, format!(
40+
Err(Error::other(format!(
4141
"Error: out of bounds memory {} (insn #{:?}), addr {:#x}, size {:?}\nmbuff: {:#x}/{:#x}, mem: {:#x}/{:#x}, stack: {:#x}/{:#x}",
4242
access_type, insn_ptr, addr, len,
4343
mbuff.as_ptr() as u64, mbuff.len(),
@@ -59,8 +59,7 @@ pub fn execute_program(
5959

6060
let (prog, stack_usage) = match prog_ {
6161
Some(prog) => (prog, stack_usage.unwrap()),
62-
None => Err(Error::new(
63-
ErrorKind::Other,
62+
None => Err(Error::other(
6463
"Error: No program set, call prog_set() to load one",
6564
))?,
6665
};
@@ -405,8 +404,7 @@ pub fn execute_program(
405404
if let Some(function) = helpers.get(&(insn.imm as u32)) {
406405
reg[0] = function(reg[1], reg[2], reg[3], reg[4], reg[5]);
407406
} else {
408-
Err(Error::new(
409-
ErrorKind::Other,
407+
Err(Error::other(
410408
format!(
411409
"Error: unknown helper function (id: {:#x})",
412410
insn.imm as u32
@@ -417,8 +415,7 @@ pub fn execute_program(
417415
// eBPF-to-eBPF call
418416
1 => {
419417
if stack_frame_idx >= MAX_CALL_DEPTH {
420-
Err(Error::new(
421-
ErrorKind::Other,
418+
Err(Error::other(
422419
format!(
423420
"Error: too many nested calls (max: {MAX_CALL_DEPTH})"
424421
)
@@ -434,8 +431,7 @@ pub fn execute_program(
434431
insn_ptr += insn.imm as usize;
435432
}
436433
_ => {
437-
Err(Error::new(
438-
ErrorKind::Other,
434+
Err(Error::other(
439435
format!("Error: unsupported call type #{} (insn #{})",
440436
_src,
441437
insn_ptr-1

src/jit.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
#![allow(clippy::single_match)]
99

10-
use crate::{ebpf, format, vec, Error, ErrorKind, HashMap, Vec};
10+
use crate::{ebpf, format, vec, Error, HashMap, Vec};
11+
#[cfg(not(feature = "std"))]
12+
use crate::ErrorKind;
1113
use core::fmt::Error as FormatterError;
1214
use core::fmt::Formatter;
1315
use core::mem;
@@ -921,8 +923,7 @@ impl JitCompiler {
921923
self.emit_mov(mem, R9, RCX);
922924
self.emit_call(mem, *helper as usize);
923925
} else {
924-
Err(Error::new(
925-
ErrorKind::Other,
926+
Err(Error::other(
926927
format!(
927928
"[JIT] Error: unknown helper function (id: {:#x})",
928929
insn.imm as u32
@@ -935,8 +936,7 @@ impl JitCompiler {
935936
self.emit_local_call(mem, target_pc);
936937
}
937938
_ => {
938-
Err(Error::new(
939-
ErrorKind::Other,
939+
Err(Error::other(
940940
format!(
941941
"[JIT] Error: unexpected call type #{:?} (insn #{insn_ptr:?})",
942942
insn.src
@@ -951,8 +951,7 @@ impl JitCompiler {
951951
}
952952

953953
_ => {
954-
Err(Error::new(
955-
ErrorKind::Other,
954+
Err(Error::other(
956955
format!(
957956
"[JIT] Error: unknown eBPF opcode {:#2x} (insn #{insn_ptr:?})",
958957
insn.opc

src/lib.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -505,8 +505,7 @@ impl<'a> EbpfVmMbuff<'a> {
505505
pub fn jit_compile(&mut self) -> Result<(), Error> {
506506
let prog = match self.prog {
507507
Some(prog) => prog,
508-
None => Err(Error::new(
509-
ErrorKind::Other,
508+
None => Err(Error::other(
510509
"Error: No program set, call prog_set() to load one",
511510
))?,
512511
};
@@ -612,8 +611,7 @@ impl<'a> EbpfVmMbuff<'a> {
612611
0,
613612
0,
614613
)),
615-
None => Err(Error::new(
616-
ErrorKind::Other,
614+
None => Err(Error::other(
617615
"Error: program has not been JIT-compiled",
618616
)),
619617
}
@@ -1095,7 +1093,7 @@ impl<'a> EbpfVmFixedMbuff<'a> {
10951093
let l = self.mbuff.buffer.len();
10961094
// Can this ever happen? Probably not, should be ensured at mbuff creation.
10971095
if self.mbuff.data_offset + 8 > l || self.mbuff.data_end_offset + 8 > l {
1098-
Err(Error::new(ErrorKind::Other, format!("Error: buffer too small ({:?}), cannot use data_offset {:?} and data_end_offset {:?}",
1096+
Err(Error::other(format!("Error: buffer too small ({:?}), cannot use data_offset {:?} and data_end_offset {:?}",
10991097
l, self.mbuff.data_offset, self.mbuff.data_end_offset)))?;
11001098
}
11011099
LittleEndian::write_u64(
@@ -1136,8 +1134,7 @@ impl<'a> EbpfVmFixedMbuff<'a> {
11361134
pub fn jit_compile(&mut self) -> Result<(), Error> {
11371135
let prog = match self.parent.prog {
11381136
Some(prog) => prog,
1139-
None => Err(Error::new(
1140-
ErrorKind::Other,
1137+
None => Err(Error::other(
11411138
"Error: No program set, call prog_set() to load one",
11421139
))?,
11431140
};
@@ -1233,8 +1230,7 @@ impl<'a> EbpfVmFixedMbuff<'a> {
12331230
self.mbuff.data_offset,
12341231
self.mbuff.data_end_offset,
12351232
)),
1236-
None => Err(Error::new(
1237-
ErrorKind::Other,
1233+
None => Err(Error::other(
12381234
"Error: program has not been JIT-compiled",
12391235
)),
12401236
}
@@ -1656,8 +1652,7 @@ impl<'a> EbpfVmRaw<'a> {
16561652
pub fn jit_compile(&mut self) -> Result<(), Error> {
16571653
let prog = match self.parent.prog {
16581654
Some(prog) => prog,
1659-
None => Err(Error::new(
1660-
ErrorKind::Other,
1655+
None => Err(Error::other(
16611656
"Error: No program set, call prog_set() to load one",
16621657
))?,
16631658
};

src/verifier.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ use crate::lib::*;
2323

2424
fn reject<S: AsRef<str>>(msg: S) -> Result<(), Error> {
2525
let full_msg = format!("[Verifier] Error: {}", msg.as_ref());
26-
Err(Error::new(ErrorKind::Other, full_msg))
26+
Err(Error::other(full_msg))
2727
}
2828

2929
fn check_prog_len(prog: &[u8]) -> Result<(), Error> {
30-
if prog.len() % ebpf::INSN_SIZE != 0 {
30+
if !prog.len().is_multiple_of(ebpf::INSN_SIZE) {
3131
reject(format!(
3232
"eBPF program length must be a multiple of {:?} octets",
3333
ebpf::INSN_SIZE

tests/misc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ extern crate rbpf;
1818
use rbpf::assembler::assemble;
1919
#[cfg(feature = "std")]
2020
use rbpf::helpers;
21-
use rbpf::lib::{Error, ErrorKind};
21+
use rbpf::lib::Error;
2222

2323
// The following two examples have been compiled from C with the following command:
2424
//
@@ -658,7 +658,7 @@ fn verifier_success(_prog: &[u8]) -> Result<(), Error> {
658658
}
659659

660660
fn verifier_fail(_prog: &[u8]) -> Result<(), Error> {
661-
Err(Error::new(ErrorKind::Other, "Gaggablaghblagh!"))
661+
Err(Error::other("Gaggablaghblagh!"))
662662
}
663663

664664
#[test]

0 commit comments

Comments
 (0)