Kaynağa Gözat

Avoid unnecessary references or vectors

All found by rust-clippy
Jan-Erik Rediger 8 yıl önce
ebeveyn
işleme
8d7042d5cc
3 değiştirilmiş dosya ile 15 ekleme ve 18 silme
  1. 2 3
      src/ebpf.rs
  2. 2 2
      src/jit.rs
  3. 11 13
      src/lib.rs

+ 2 - 3
src/ebpf.rs

@@ -413,7 +413,7 @@ pub fn get_insn(prog: &[u8], idx: usize) -> Insn {
         panic!("Error: cannot reach instruction at index {:?} in program containing {:?} bytes",
                idx, prog.len());
     }
-    let insn = Insn {
+    Insn {
         opc:  prog[INSN_SIZE * idx],
         dst:  prog[INSN_SIZE * idx + 1] & 0x0f,
         src: (prog[INSN_SIZE * idx + 1] & 0xf0) >> 4,
@@ -423,8 +423,7 @@ pub fn get_insn(prog: &[u8], idx: usize) -> Insn {
         imm: unsafe {
             let x = prog.as_ptr().offset((INSN_SIZE * idx + 4) as isize) as *const i32; *x
         },
-    };
-    insn
+    }
 }
 
 /// Return a vector of `struct Insn` built from a program.

+ 2 - 2
src/jit.rs

@@ -714,10 +714,10 @@ impl<'a> JitMemory<'a> {
                     // For JIT, helpers in use MUST be registered at compile time. They can be
                     // updated later, but not created after compiling (we need the address of the
                     // helper function in the JIT-compiled program).
-                    if let Some(_) = helpers.get(&(insn.imm as u32)) {
+                    if let Some(helper) = helpers.get(&(insn.imm as u32)) {
                         // We reserve RCX for shifts
                         emit_mov(self, R9, RCX);
-                        emit_call(self, helpers[&(insn.imm as u32)] as i64);
+                        emit_call(self, *helper as i64);
                     } else {
                         panic!("[JIT] Error: unknown helper function (id: {:#x})",
                                insn.imm as u32);

+ 11 - 13
src/lib.rs

@@ -98,9 +98,8 @@ impl<'a> EbpfVmMbuff<'a> {
     pub fn new(prog: &'a [u8]) -> EbpfVmMbuff<'a> {
         verifier::check(prog);
 
-        #[allow(unused_variables)]
-        fn no_jit(foo: *mut u8, foo_len: usize, bar: *mut u8, bar_len: usize,
-                  nodata_offset: usize, nodata_end_offset: usize) -> u64 {
+        fn no_jit(_mbuff: *mut u8, _len: usize, _mem: *mut u8, _mem_len: usize,
+                  _nodata_offset: usize, _nodata_end_offset: usize) -> u64 {
             panic!("Error: program has not been JIT-compiled");
         }
 
@@ -237,10 +236,10 @@ impl<'a> EbpfVmMbuff<'a> {
         }
 
         let check_mem_load = | addr: u64, len: usize, insn_ptr: usize | {
-            EbpfVmMbuff::check_mem(addr, len, "load", insn_ptr, &mbuff, &mem, &stack);
+            EbpfVmMbuff::check_mem(addr, len, "load", insn_ptr, mbuff, mem, &stack);
         };
         let check_mem_store = | addr: u64, len: usize, insn_ptr: usize | {
-            EbpfVmMbuff::check_mem(addr, len, "store", insn_ptr, &mbuff, &mem, &stack);
+            EbpfVmMbuff::check_mem(addr, len, "store", insn_ptr, mbuff, mem, &stack);
         };
 
         // Loop on instructions
@@ -509,7 +508,7 @@ impl<'a> EbpfVmMbuff<'a> {
     /// vm.jit_compile();
     /// ```
     pub fn jit_compile(&mut self) {
-        self.jit = jit::compile(&self.prog, &self.helpers, true, false);
+        self.jit = jit::compile(self.prog, &self.helpers, true, false);
     }
 
     /// Execute the previously JIT-compiled program, with the given packet data and metadata
@@ -834,7 +833,7 @@ impl<'a> EbpfVmFixedMbuff<'a> {
             *data     = mem.as_ptr() as u64;
             *data_end = mem.as_ptr() as u64 + mem.len() as u64;
         }
-        self.parent.prog_exec(mem, &mut self.mbuff.buffer)
+        self.parent.prog_exec(mem, &self.mbuff.buffer)
     }
 
     /// JIT-compile the loaded program. No argument required for this.
@@ -866,7 +865,7 @@ impl<'a> EbpfVmFixedMbuff<'a> {
     /// vm.jit_compile();
     /// ```
     pub fn jit_compile(&mut self) {
-        self.parent.jit = jit::compile(&self.parent.prog, &self.parent.helpers, true, true);
+        self.parent.jit = jit::compile(self.parent.prog, &self.parent.helpers, true, true);
     }
 
     /// Execute the previously JIT-compiled program, with the given packet data, in a manner very
@@ -1090,8 +1089,7 @@ impl<'a> EbpfVmRaw<'a> {
     /// assert_eq!(res, 0x22cc);
     /// ```
     pub fn prog_exec(&self, mem: &'a mut [u8]) -> u64 {
-        let mut mbuff = vec![];
-        self.parent.prog_exec(mem, &mut mbuff)
+        self.parent.prog_exec(mem, &[])
     }
 
     /// JIT-compile the loaded program. No argument required for this.
@@ -1119,7 +1117,7 @@ impl<'a> EbpfVmRaw<'a> {
     /// vm.jit_compile();
     /// ```
     pub fn jit_compile(&mut self) {
-        self.parent.jit = jit::compile(&self.parent.prog, &self.parent.helpers, false, false);
+        self.parent.jit = jit::compile(self.parent.prog, &self.parent.helpers, false, false);
     }
 
     /// Execute the previously JIT-compiled program, with the given packet data, in a manner very
@@ -1357,7 +1355,7 @@ impl<'a> EbpfVmNoData<'a> {
     /// assert_eq!(res, 0x1122);
     /// ```
     pub fn prog_exec(&self) -> u64 {
-        self.parent.prog_exec(&mut vec![])
+        self.parent.prog_exec(&mut [])
     }
 
     /// Execute the previously JIT-compiled program, without providing pointers to any memory area
@@ -1395,6 +1393,6 @@ impl<'a> EbpfVmNoData<'a> {
     /// }
     /// ```
     pub unsafe fn prog_exec_jit(&self) -> u64 {
-        self.parent.prog_exec_jit(&mut vec![])
+        self.parent.prog_exec_jit(&mut [])
     }
 }