ソースを参照

Merge pull request #22 from waywardmonkeys/lint_warning_fixes

Lint and warning fixes
Quentin Monnet 7 年 前
コミット
6125784e9b
13 ファイル変更29 行追加27 行削除
  1. 0 1
      Cargo.toml
  2. 0 2
      README.md
  3. 1 0
      clippy.toml
  4. 2 2
      examples/load_elf.rs
  5. 1 2
      examples/to_json.rs
  6. 2 2
      src/disassembler.rs
  7. 5 5
      src/jit.rs
  8. 5 4
      src/lib.rs
  9. 1 1
      src/verifier.rs
  10. 2 1
      tests/assembler.rs
  11. 8 7
      tests/misc.rs
  12. 1 0
      tests/ubpf_jit_x86_64.rs
  13. 1 0
      tests/ubpf_vm.rs

+ 0 - 1
Cargo.toml

@@ -30,6 +30,5 @@ time = "0.1"
 
 [dev-dependencies]
 
-byteorder = "0.5.3"
 elf = "0.0.10"
 json = "0.11.4"

+ 0 - 2
README.md

@@ -341,7 +341,6 @@ to your `Cargo.toml` file.
 ```toml
 [dependencies]
 rbpf = "0.0.2"
-byteorder = "0.5.3"
 elf = "0.0.10"
 ```
 
@@ -355,7 +354,6 @@ in charger the buffer update so that we only have to provide a reference to the
 packet data for each run of the program.
 
 ```rust
-extern crate byteorder;
 extern crate elf;
 use std::path::PathBuf;
 

+ 1 - 0
clippy.toml

@@ -0,0 +1 @@
+doc-valid-idents = ["eBPF", "uBPF"] 

+ 2 - 2
examples/load_elf.rs

@@ -4,8 +4,8 @@
 // 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))]
 
-extern crate byteorder;
 extern crate elf;
 use std::path::PathBuf;
 
@@ -89,7 +89,7 @@ fn main() {
         0x64, 0x66, 0x0au8
     ];
 
-    let mut packet2 = &mut [
+    let packet2 = &mut [
         0x01, 0x23, 0x45, 0x67, 0x89, 0xab,
         0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54,
         0x08, 0x00, // ethertype

+ 1 - 2
examples/to_json.rs

@@ -8,7 +8,6 @@
 #[macro_use]
 extern crate json;
 
-extern crate byteorder;
 extern crate elf;
 use std::path::PathBuf;
 
@@ -75,5 +74,5 @@ fn main() {
 
     let prog = &text_scn.data;
 
-    println!("{}", to_json(&prog));
+    println!("{}", to_json(prog));
 }

+ 2 - 2
src/disassembler.rs

@@ -154,7 +154,7 @@ pub fn to_insn_vec(prog: &[u8]) -> Vec<HLInsn> {
         panic!("[Disassembler] Error: eBPF program length must be a multiple of {:?} octets",
                ebpf::INSN_SIZE);
     }
-    if prog.len() == 0 {
+    if prog.is_empty() {
         return vec![];
     }
 
@@ -339,7 +339,7 @@ pub fn disassemble(prog: &[u8]) {
         panic!("[Disassembler] Error: eBPF program length must be a multiple of {:?} octets",
                ebpf::INSN_SIZE);
     }
-    if prog.len() == 0 {
+    if prog.is_empty() {
         return;
     }
 

+ 5 - 5
src/jit.rs

@@ -115,7 +115,7 @@ fn emit_jump_offset(jit: &mut JitMemory, target_pc: isize) {
 
 #[inline]
 fn emit_modrm(jit: &mut JitMemory, modrm: u8, r: u8, m: u8) {
-    assert!((modrm | 0xc0) == 0xc0);
+    assert_eq!((modrm | 0xc0), 0xc0);
     emit1(jit, (modrm & 0xc0) | ((r & 0b111) << 3) | (m & 0b111));
 }
 
@@ -139,10 +139,10 @@ fn emit_modrm_and_displacement(jit: &mut JitMemory, r: u8, m: u8, d: i32) {
 
 #[inline]
 fn emit_rex(jit: &mut JitMemory, w: u8, r: u8, x: u8, b: u8) {
-    assert!((w | 1) == 1);
-    assert!((r | 1) == 1);
-    assert!((x | 1) == 1);
-    assert!((b | 1) == 1);
+    assert_eq!((w | 1), 1);
+    assert_eq!((r | 1), 1);
+    assert_eq!((x | 1), 1);
+    assert_eq!((b | 1), 1);
     emit1(jit, 0x40 | (w << 3) | (r << 2) | (x << 1) | b);
 }
 

+ 5 - 4
src/lib.rs

@@ -14,13 +14,14 @@
        html_favicon_url = "https://raw.githubusercontent.com/qmonnet/rbpf/master/misc/rbpf.ico")]
 
 #![warn(missing_docs)]
+// There are unused mut warnings due to unsafe code.
+#![allow(unused_mut)]
 
-#![cfg_attr(feature = "cargo-clippy", allow(doc_markdown, match_same_arms))]
+#![cfg_attr(feature = "cargo-clippy", allow(cast_lossless, doc_markdown, match_same_arms, unreadable_literal))]
 
 use std::u32;
 use std::collections::HashMap;
 
-extern crate libc;
 extern crate combine;
 extern crate time;
 
@@ -237,10 +238,10 @@ impl<'a> EbpfVmMbuff<'a> {
         let mut reg: [u64;11] = [
             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, stack.as_ptr() as u64 + stack.len() as u64
         ];
-        if mbuff.len() > 0 {
+        if !mbuff.is_empty() {
             reg[1] = mbuff.as_ptr() as u64;
         }
-        else if mem.len() > 0 {
+        else if !mem.is_empty() {
             reg[1] = mem.as_ptr() as u64;
         }
 

+ 1 - 1
src/verifier.rs

@@ -35,7 +35,7 @@ fn check_prog_len(prog: &[u8]) {
                ebpf::PROG_MAX_INSNS, prog.len() / ebpf::INSN_SIZE);
     }
 
-    if prog.len() == 0 {
+    if prog.is_empty() {
         panic!("[Verifier] Error: program does not end with “EXIT” instruction");
     }
     let last_insn = ebpf::get_insn(prog, (prog.len() / ebpf::INSN_SIZE) - 1);

+ 2 - 1
tests/assembler.rs

@@ -4,6 +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))]
 
 extern crate rbpf;
 mod common;
@@ -436,7 +437,7 @@ fn test_large_immediate() {
 
 #[test]
 fn test_tcp_sack() {
-    assert_eq!(assemble(&TCP_SACK_ASM), Ok(TCP_SACK_BIN.to_vec()));
+    assert_eq!(assemble(TCP_SACK_ASM), Ok(TCP_SACK_BIN.to_vec()));
 }
 
 #[test]

+ 8 - 7
tests/misc.rs

@@ -4,18 +4,19 @@
 // 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.
 
-
-// These crates would be needed to load bytecode from a BPF-compiled object file. Since the crates
-// are not used anywhere else in the library, it is deactivated: we do not want to load and compile
-// them just for the tests. If you want to use them, do not forget to add the following
-// dependencies to your Cargo.toml file:
+// There are unused mut warnings due to unsafe code.
+#![allow(unused_mut)]
+#![cfg_attr(feature = "cargo-clippy", allow(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
+// it just for the tests. If you want to use it, do not forget to add the following
+// dependency to your Cargo.toml file:
 //
 // ---
-// byteorder = "0.5.3"
 // elf = "0.0.10"
 // ---
 //
-// extern crate byteorder;
 // extern crate elf;
 // use std::path::PathBuf;
 

+ 1 - 0
tests/ubpf_jit_x86_64.rs

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

+ 1 - 0
tests/ubpf_vm.rs

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