» Make grep CLI App in Rust » 2. Development » 2.5 Add Lint

Add Lint

Lint refers to a tool or program that analyzes source code to find and report issues related to programming style, potential errors, and adherence to coding standards. The term "lint" originates from the name of a Unix utility called "lint" that was used to identify bugs and errors in C programming code.

Using linting tools in a Rust project is a good practice for maintaining code quality and consistency. Linting tools analyze your code for potential errors, style issues, and adherence to coding standards.

Usually, developers use clippy to lint a Rust project.

If you don't have it, install it using:

rustup update
rustup component add clippy

Know more about rustup at: https://rustup.rs/

Run the linting:

cargo clippy

You will get a list of lint issues:

    Checking lr_grustep v0.1.0 (.../lr_grustep)
warning: this expression creates a reference which is immediately dereferenced by the compiler
  --> src/lib.rs:61:40
   |
61 |             let result = grep(pattern, &file_path, options)?;
   |                                        ^^^^^^^^^^ help: change this to: `file_path`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
   = note: `#[warn(clippy::needless_borrow)]` on by default

warning: `filter_map()` will run forever if the iterator repeatedly produces an `Err`
  --> src/lib.rs:71:23
   |
71 |     Ok(reader.lines().filter_map(|line| line.ok()).collect())
   |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `map_while(Result::ok)`
   |
note: this expression returning a `std::io::Lines` may produce an infinite number of `Err` in case of a read error
  --> src/lib.rs:71:8
   |
71 |     Ok(reader.lines().filter_map(|line| line.ok()).collect())
   |        ^^^^^^^^^^^^^^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#lines_filter_map_ok
   = note: `#[warn(clippy::lines_filter_map_ok)]` on by default

warning: `lr_grustep` (lib) generated 2 warnings (run `cargo clippy --fix --lib -p lr_grustep` to apply 1 suggestion)
    Finished dev [unoptimized + debuginfo] target(s) in 0.59s

Try to fix all of them and run clippy again.

cargo clippy

If nothing pops out, your code is in "good style" now.

Changes in src/lib.rs:

@@ -58,7 +58,7 @@ pub fn grep_recursive(
         let entry = entry?;
         if entry.file_type().is_file() {
             let file_path = entry.path();
-            let result = grep(pattern, &file_path, options)?;
+            let result = grep(pattern, file_path, options)?;
             results.extend(result);
         }
     }
@@ -68,7 +68,7 @@ pub fn grep_recursive(
 fn read_file_lines(file_path: &Path) -> Result<Vec<String>, io::Error> {
     let file = fs::File::open(file_path)?;
     let reader = io::BufReader::new(file);
-    Ok(reader.lines().filter_map(|line| line.ok()).collect())
+    Ok(reader.lines().map_while(Result::ok).collect())
 }
 
 fn filter_lines(pattern: &Regex, lines: &[String], flag: bool) -> Vec<MatchItem> {
PrevNext