Repository: xd009642/tarpaulin
Pull request #1318 (2023-07-02 10:34:09)

Ignores all single line comments
<!--- When contributing to tarpaulin all contributions should branch off the -->
<!--- develop branch as master is only used for releases. --> 

<!--- Check CONTRIBUTING.md for more information-->

<!--- Please describe the changes in the PR and them motivation below -->



Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index cb3c8b0..912ed3c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,7 @@ file.
 ### Changed
 - Expand doc test prefix to cover more of the directory tree to work with the new naming structure
 - Handle -A -W and -D flags in the RUSTFLAGS deduplication
+- Ignore `//` comments as well as `///`
 
 ## [0.26.0] 2023-06-16
 ### Changed
diff --git a/src/source_analysis/mod.rs b/src/source_analysis/mod.rs
index 7670fec..7efe1d8 100644
--- a/src/source_analysis/mod.rs
+++ b/src/source_analysis/mod.rs
@@ -58,6 +58,20 @@ pub struct LineAnalysis {
     max_line: usize,
 }
 
+/// Provides context to the source analysis stage including the tarpaulin
+/// config and the source code being analysed.
+pub(crate) struct Context<'a> {
+    /// Program config
+    config: &'a Config,
+    /// Contents of the source file
+    file_contents: &'a str,
+    /// path to the file being analysed
+    file: &'a Path,
+    /// Other parts of context are immutable like tarpaulin config and users
+    /// source code. This is discovered during hence use of interior mutability
+    ignore_mods: RefCell<HashSet<PathBuf>>,
+}
+
 /// When the `LineAnalysis` results are mapped to their files there needs to be
 /// an easy way to get the information back. For the container used implement
 /// this trait
@@ -393,10 +407,10 @@ impl SourceAnalysis {
     /// Finds lines from the raw string which are ignorable.
     /// These are often things like close braces, semicolons that may register as
     /// false positives.
-    fn find_ignorable_lines(&mut self, ctx: &Context) {
+    pub(crate) fn find_ignorable_lines(&mut self, ctx: &Context) {
         lazy_static! {
             static ref IGNORABLE: Regex =
-                Regex::new(r"^((\s*///)|([\[\]\{\}\(\)\s;\?,/]*$))").unwrap();
+                Regex::new(r"^((\s*//)|([\[\]\{\}\(\)\s;\?,/]*$))").unwrap();
         }
         let analysis = self.get_line_analysis(ctx.file.to_path_buf());
         let lines = ctx
@@ -429,6 +443,7 @@ impl SourceAnalysis {
 
     /// Printout a debug summary of the results of source analysis if debug logging
     /// is enabled
+    #[cfg(not(tarpaulin_include))]
     pub fn debug_printout(&self, config: &Config) {
         if config.debug {
             for (path, analysis) in &self.lines {
@@ -486,17 +501,3 @@ fn maybe_ignore_first_line(file: &Path, result: &mut HashMap<PathBuf, LineAnalys
         }
     }
 }
-
-/// Provides context to the source analysis stage including the tarpaulin
-/// config and the source code being analysed.
-pub(crate) struct Context<'a> {
-    /// Program config
-    config: &'a Config,
-    /// Contents of the source file
-    file_contents: &'a str,
-    /// path to the file being analysed
-    file: &'a Path,
-    /// Other parts of context are immutable like tarpaulin config and users
-    /// source code. This is discovered during hence use of interior mutability
-    ignore_mods: RefCell<HashSet<PathBuf>>,
-}


Test patch:
diff --git a/src/source_analysis/tests.rs b/src/source_analysis/tests.rs
index b95c39d..9a65991 100644
--- a/src/source_analysis/tests.rs
+++ b/src/source_analysis/tests.rs
@@ -1516,3 +1516,37 @@ fn visit_generics() {
     assert!(lines.ignore.contains(&Lines::Line(19)));
     assert!(lines.ignore.contains(&Lines::Line(20)));
 }
+
+#[test]
+fn ignore_comment() {
+    let config = Config::default();
+    let ctx = Context {
+        config: &config,
+        file_contents: "/// I am a doc comment
+        fn foo() -> u32 {
+            let x = 5;
+            // I should be ignored
+            // and me as well
+            x * 2
+        }
+        
+        fn blah() 
+        {
+
+        }",
+        file: Path::new(""),
+        ignore_mods: RefCell::new(HashSet::new()),
+    };
+    let mut analysis = SourceAnalysis::new();
+    analysis.find_ignorable_lines(&ctx);
+    let lines = &analysis.lines[Path::new("")];
+    assert_eq!(lines.ignore.len(), 8);
+    assert!(lines.ignore.contains(&Lines::Line(1)));
+    assert!(lines.ignore.contains(&Lines::Line(4)));
+    assert!(lines.ignore.contains(&Lines::Line(5)));
+    assert!(lines.ignore.contains(&Lines::Line(7)));
+    assert!(lines.ignore.contains(&Lines::Line(8)));
+    assert!(lines.ignore.contains(&Lines::Line(10)));
+    assert!(lines.ignore.contains(&Lines::Line(11)));
+    assert!(lines.ignore.contains(&Lines::Line(12)));
+}
diff --git a/tests/line_coverage.rs b/tests/line_coverage.rs
index 7ad4d2b..9568b9f 100644
--- a/tests/line_coverage.rs
+++ b/tests/line_coverage.rs
@@ -45,7 +45,7 @@ fn simple_project_coverage() {
         if l.line == 6 {
             assert_eq!(CoverageStat::Line(0), l.stats);
         } else if l.line == 8 {
-            assert_eq!(CoverageStat::Line(1), l.stats);
+            assert!(matches!(l.stats, CoverageStat::Line(c) if c > 0 ));
         }
     }
 }