فهرست منبع

Let cargo-clippy lint the code

Run "cargo clippy --fix" and commit the changes.
Quentin Monnet 2 سال پیش
والد
کامیت
153995b3a3
13فایلهای تغییر یافته به همراه24 افزوده شده و 24 حذف شده
  1. 1 1
      examples/load_elf.rs
  2. 2 2
      src/asm_parser.rs
  3. 3 3
      src/assembler.rs
  4. 1 1
      src/disassembler.rs
  5. 2 2
      src/ebpf.rs
  6. 2 2
      src/insn_builder.rs
  7. 1 1
      src/jit.rs
  8. 2 2
      src/verifier.rs
  9. 6 6
      tests/assembler.rs
  10. 1 1
      tests/common.rs
  11. 1 1
      tests/misc.rs
  12. 1 1
      tests/ubpf_jit_x86_64.rs
  13. 1 1
      tests/ubpf_vm.rs

+ 1 - 1
examples/load_elf.rs

@@ -4,7 +4,7 @@
 // the MIT license <http://opensource.org/licenses/MIT>, at your option. This file may not be
 // copied, modified, or distributed except according to those terms.
 
-#![cfg_attr(feature = "cargo-clippy", allow(unreadable_literal))]
+#![cfg_attr(feature = "cargo-clippy", allow(clippy::unreadable_literal))]
 
 extern crate elf;
 use std::path::PathBuf;

+ 2 - 2
src/asm_parser.rs

@@ -16,7 +16,7 @@ use combine::{between, eof, many, many1, one_of, optional, Parser, ParseError, P
 use combine::primitives::{Error, Info};
 
 /// Operand of an instruction.
-#[derive(Clone, Copy, Debug, PartialEq)]
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
 pub enum Operand {
     /// Register number.
     Register(i64),
@@ -29,7 +29,7 @@ pub enum Operand {
 }
 
 /// Parsed instruction.
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, Eq)]
 pub struct Instruction {
     /// Instruction name.
     pub name: String,

+ 3 - 3
src/assembler.rs

@@ -121,16 +121,16 @@ fn make_instruction_map() -> HashMap<String, (InstructionType, u8)> {
 }
 
 fn insn(opc: u8, dst: i64, src: i64, off: i64, imm: i64) -> Result<Insn, String> {
-    if dst < 0 || dst >= 16 {
+    if !(0..16).contains(&dst) {
         return Err(format!("Invalid destination register {}", dst));
     }
     if dst < 0 || src >= 16 {
         return Err(format!("Invalid source register {}", src));
     }
-    if off < -32768 || off >= 32768 {
+    if !(-32768..32768).contains(&off) {
         return Err(format!("Invalid offset {}", off));
     }
-    if imm < -2147483648 || imm >= 2147483648 {
+    if !(-2147483648..2147483648).contains(&imm) {
         return Err(format!("Invalid immediate {}", imm));
     }
     Ok(Insn {

+ 1 - 1
src/disassembler.rs

@@ -99,7 +99,7 @@ fn jmp_reg_str(name: &str, insn: &ebpf::Insn) -> String {
 /// See <https://www.kernel.org/doc/Documentation/networking/filter.txt> for the Linux kernel
 /// documentation about eBPF, or <https://github.com/iovisor/bpf-docs/blob/master/eBPF.md> for a
 /// more concise version.
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, Eq)]
 pub struct HLInsn {
     /// Operation code.
     pub opc:  u8,

+ 2 - 2
src/ebpf.rs

@@ -175,7 +175,7 @@ pub const LD_IND_W   : u8 = BPF_LD    | BPF_IND | BPF_W;
 pub const LD_IND_DW  : u8 = BPF_LD    | BPF_IND | BPF_DW;
 
 #[allow(unknown_lints)]
-#[allow(eq_op)]
+#[allow(clippy::eq_op)]
 /// BPF opcode: `lddw dst, imm` /// `dst = imm`.
 pub const LD_DW_IMM  : u8 = BPF_LD    | BPF_IMM | BPF_DW;
 /// BPF opcode: `ldxb dst, [src + off]` /// `dst = (src + off) as u8`.
@@ -391,7 +391,7 @@ pub type Helper = fn (u64, u64, u64, u64, u64) -> u64;
 /// See <https://www.kernel.org/doc/Documentation/networking/filter.txt> for the Linux kernel
 /// documentation about eBPF, or <https://github.com/iovisor/bpf-docs/blob/master/eBPF.md> for a
 /// more concise version.
-#[derive(Debug, PartialEq, Clone)]
+#[derive(Debug, PartialEq, Eq, Clone)]
 pub struct Insn {
     /// Operation code.
     pub opc: u8,

+ 2 - 2
src/insn_builder.rs

@@ -362,7 +362,7 @@ impl<'i> Instruction for Move<'i> {
     }
 }
 
-#[derive(Copy, Clone, PartialEq)]
+#[derive(Copy, Clone, PartialEq, Eq)]
 /// The source of ALU and JMP instructions
 pub enum Source {
     /// immediate field will be used as a source
@@ -556,7 +556,7 @@ impl<'i> Instruction for Jump<'i> {
     }
 }
 
-#[derive(Copy, Clone, PartialEq)]
+#[derive(Copy, Clone, PartialEq, Eq)]
 /// Conditions for JMP instructions
 pub enum Cond {
     /// Absolute or unconditional

+ 1 - 1
src/jit.rs

@@ -132,7 +132,7 @@ fn emit_modrm_reg2reg(jit: &mut JitMemory, r: u8, m: u8) {
 fn emit_modrm_and_displacement(jit: &mut JitMemory, r: u8, m: u8, d: i32) {
     if d == 0 && (m & 0b111) != RBP {
         emit_modrm(jit, 0x00, r, m);
-    } else if d >= -128 && d <= 127 {
+    } else if (-128..=127).contains(&d) {
         emit_modrm(jit, 0x40, r, m);
         emit1(jit, d as u8);
     } else {

+ 2 - 2
src/verifier.rs

@@ -42,11 +42,11 @@ fn check_prog_len(prog: &[u8]) -> Result<(), Error> {
     }
 
     if prog.is_empty() {
-        reject("no program set, call set_program() to load one".to_string())?;
+        reject("no program set, call set_program() to load one")?;
     }
     let last_insn = ebpf::get_insn(prog, (prog.len() / ebpf::INSN_SIZE) - 1);
     if last_insn.opc != ebpf::EXIT {
-        reject("program does not end with “EXIT” instruction".to_string())?;
+        reject("program does not end with “EXIT” instruction")?;
     }
 
     Ok(())

+ 6 - 6
tests/assembler.rs

@@ -4,7 +4,7 @@
 // the MIT license <http://opensource.org/licenses/MIT>, at your option. This file may not be
 // copied, modified, or distributed except according to those terms.
 
-#![cfg_attr(feature = "cargo-clippy", allow(unreadable_literal))]
+#![cfg_attr(feature = "cargo-clippy", allow(clippy::unreadable_literal))]
 
 extern crate rbpf;
 mod common;
@@ -19,11 +19,11 @@ fn asm(src: &str) -> Result<Vec<ebpf::Insn>, String> {
 
 fn insn(opc: u8, dst: u8, src: u8, off: i16, imm: i32) -> ebpf::Insn {
     ebpf::Insn {
-        opc: opc,
-        dst: dst,
-        src: src,
-        off: off,
-        imm: imm,
+        opc,
+        dst,
+        src,
+        off,
+        imm,
     }
 }
 

+ 1 - 1
tests/common.rs

@@ -10,7 +10,7 @@
 // Assembly code and data for tcp_sack testcases.
 
 #[allow(dead_code)]
-pub const TCP_SACK_ASM: &'static str = "
+pub const TCP_SACK_ASM: &str = "
     ldxb r2, [r1+12]
     ldxb r3, [r1+13]
     lsh r3, 0x8

+ 1 - 1
tests/misc.rs

@@ -6,7 +6,7 @@
 
 // There are unused mut warnings due to unsafe code.
 #![allow(unused_mut)]
-#![cfg_attr(feature = "cargo-clippy", allow(unreadable_literal))]
+#![cfg_attr(feature = "cargo-clippy", allow(clippy::unreadable_literal))]
 
 // This crate would be needed to load bytecode from a BPF-compiled object file. Since the crate
 // is not used anywhere else in the library, it is deactivated: we do not want to load and compile

+ 1 - 1
tests/ubpf_jit_x86_64.rs

@@ -20,7 +20,7 @@
 
 // These are unit tests for the eBPF JIT compiler.
 
-#![cfg_attr(feature = "cargo-clippy", allow(unreadable_literal))]
+#![cfg_attr(feature = "cargo-clippy", allow(clippy::unreadable_literal))]
 #![cfg(not(windows))]
 
 extern crate rbpf;

+ 1 - 1
tests/ubpf_vm.rs

@@ -20,7 +20,7 @@
 
 // These are unit tests for the eBPF interpreter.
 
-#![cfg_attr(feature = "cargo-clippy", allow(unreadable_literal))]
+#![cfg_attr(feature = "cargo-clippy", allow(clippy::unreadable_literal))]
 
 extern crate rbpf;
 mod common;