kbknapp/cargo-count

C and C Headers have some unsafe code

kbknapp opened this issue · 8 comments

Since the unsafe counter is naive and counts all C and C++ as unsafe it's strange that some code in .c and .h files is not unsafe. See the example in the README.md...

May be some problem with blank lines? Can you add examples ro repo?

I noticed it in the Rust repo (See the example output), but I haven't yet done any real investigating such as creating a quick .c file and seeing what happens.

It seems to be the same bug as #2.

int  test() {
    return /* test */ 0;    
}

Result is

         Language  Files  Lines  Blanks  Comments  Code  Unsafe (%)
         --------  -----  -----  ------  --------  ----  ----------
         C Header  1      3      0       1         2     1 (50.00%)
         TOML      1      4      0       0         4     
         --------  -----  -----  ------  --------  ----  ----------
Totals:            2      7      0       1         6     1 (16.67%)

Makes sense, and now I see why I didn't catch it before! Thanks! 😄

#2 found some of the erroneous lines...but not all.

The only place, where count.code increments before unsafe check is

else if line.contains(ms) {
    debugln!("line contains a multi start");
    count.code += 1;
    is_in_comments = line.contains(count.multi_end().unwrap());
    debugln!("line also contained a multi end: {:?}", is_in_comments);
    continue;
    }

and there is continue

Let me fix it?

That was it! Sorry, I just pushed the patch!