kbknapp / cargo-count Goto Github PK
View Code? Open in Web Editor NEWa cargo subcommand for counting lines of code in Rust projects
License: MIT License
a cargo subcommand for counting lines of code in Rust projects
License: MIT License
Updating git repository `https://github.com/kbknapp/cargo-count`
Installing cargo-count v0.2.4 (https://github.com/kbknapp/cargo-count#eebe6f87)
Updating crates.io index
error: failed to compile `cargo-count v0.2.4 (https://github.com/kbknapp/cargo-count#eebe6f87)`, intermediate artifacts can be found at `/tmp/cargo-installSJ1NfQ`
Caused by:
failed to select a version for the requirement `clap = "~2.11.2"`
candidate versions found which didn't match: 2.33.3, 2.33.2, 2.33.1, ...
location searched: crates.io index
required by package `cargo-count v0.2.4 (/home/cheng/.cargo/git/checkouts/cargo-count-01e28dcffdb7943b/eebe6f8)`
Single-line block comment counts erroneously. For this sample
/* */
fn main() {
println!("Hello world");
}
I got
Language Files Lines Blanks Comments Code Unsafe (%)
-------- ----- ----- ------ -------- ---- ----------
Rust 1 4 0 4 0 0 (NaN%)
TOML 1 4 0 0 4
-------- ----- ----- ------ -------- ---- ----------
Totals: 2 8 0 4 4 0 (0.00%)
Hello @kbknapp ๐ This project has a considerable number of stars (so likely has many users), but has not seen a commit in over 4 years. There's a few unresolved issues with the crate that are causing unsafe statistics to be wrongly reported, and and a handful of PRs still need to be reviewed and merged.
Are you open to adding new maintainers to the project? I'd like to see some of these issues closed. ๐
Thanks, Sean
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
...
The unsafe counter probably isn't counting all the unsafe lines due to a crappy regex, and maybe due to bad bracket counting after finding unsafe blocks.
grep
ing for unsafe {
(even those lines that don't contain //
at all, much less to say in correct places that should lead to commented out unsafe) in the Rust repo leads to ~2,000. Now consider some of those are the start of an unsafe block. So there should be around 2,000+ unsafe lines when running cargo count
, yet currently it reports only ~1,200.
I cannot get cargo-count to find any code. I have tried it in both Rust and C++ repositories, and it always outputs "No source files were found matching the specified criteria". Example:
bram@falafel% mkdir testrepo
bram@falafel% cd testrepo
bram@falafel% cargo init --bin
Created binary (application) project
bram@falafel% cat src/main.rs
fn main() {
println!("Hello, world!");
}
bram@falafel% cargo count
Gathering information...
No source files were found matching the specified criteria
Edit: additional info:
bram@falafel% cargo count -v
Gathering information...
Excluding: ["/home/bram/tmp/testrepo/.git"]
Displaying the results:
No source files were found matching the specified criteria
bram@falafel% cargo count -V
cargo-count v0.2.2
I installed cargo-count from crates.io.
Languages are simple to add.... so trying to think of a list of ones to add
$ mkdir sample
$ cd sample
$ ln -s . foo
$ cargo count
Gathering information...
thread '<main>' panicked at 'called `Result::unwrap()` on an `Err` value: Error { repr: Os { code: 40, message: "Too many levels of symbolic links" } }', src/libcore/result.rs:731
This is similar to my other issue #6, but fails for a different reason.
Overall, cargo count should probably not be following symlinks that are directories. It may or may not want to follow ones pointing to regular files.
$ mkdir sample
$ cd sample
$ ln -s invalid foo
$ cargo count
Gathering information...
thread '<main>' panicked at 'called `Result::unwrap()` on an `Err` value: Error { repr: Os { code: 2, message: "No such file or directory" } }', src/libcore/result.rs:731
The culprit seems to be the calls to metadata
in get_all_files
, which will fail on a bogus symlink. Perhaps symlink_metadata
would be better.
Similar to how cargo
works. I want this to be a CLI option though, as there may be times where you want to count something that is on your .gitignore
I narrowed this down to having any files in
.hg/store/data
that end with .d
they appear when I commit a binary of a zip or anything large to my repo
Shouldn't .git
and .hg
folders be ignored by default? or just not explode on binaries that it thinks is D source maybe
{ ".cc", ".cpp", ".cp", ".cxx", ".c++", ".C" }
As far as I know, these are all valid extensions for C++ source code, with cc
and cpp
being the most popular. Currently cargo-count
only supports cpp
.
AFAIK There is no support for GLSL
Cargo-count incorrectly reports that the following code has no instances of "unsafe":
fn test() {
unsafe { match doit() { } }
}
Gathering information...
Language Files Lines Blanks Comments Code Unsafe (%)
-------- ----- ----- ------ -------- ---- ----------
Rust 1 3 0 0 3
-------- ----- ----- ------ -------- ---- ----------
Totals: 1 3 0 0 3 0 (0.00%)
For comparison, it correctly reports that this testcase has an instance of "unsafe" in it:
fn test() {
match unsafe { doit() } { }
}
Gathering information...
Language Files Lines Blanks Comments Code Unsafe (%)
-------- ----- ----- ------ -------- ---- ----------
Rust 1 3 0 0 3 1 (33.33%)
-------- ----- ----- ------ -------- ---- ----------
Totals: 1 3 0 0 3 1 (33.33%)
I ran cargo install cargo-count
on nightly. It worked but I saw this warning:
Compiling cargo-count v0.2.1
.cargo/registry/src/github.com-1ecc6299db9ec823/cargo-count-0.2.1/src/main.rs:190:5: 190:19 warning: unused import, #[warn(unused_imports)] on by default
.cargo/registry/src/github.com-1ecc6299db9ec823/cargo-count-0.2.1/src/main.rs:190 use std::io::Write;
^~~~~~~~~~~~~~
On a project using piston with 5 source files and no unsafe code in src/
, I get this output:
$ cargo count --unsafe-statistics
Gathering information...
Language Files Lines Blanks Comments Code Unsafe (%)
-------- ----- ----- ------ -------- ---- ----------
Rust 10 28094 2972 517 24605 964 (3.92%)
TOML 1 23 3 0 20
-------- ----- ----- ------ -------- ---- ----------
Totals: 11 28117 2975 517 24625 964 (3.91%)
I believe that this is due to cargo count
taking into account files in the target/
directory, such as generated source files:
$ find . -iname '*.rs'
./target/debug/build/glutin-7fb876b2b34f427b/out/glx_bindings.rs
./target/debug/build/glutin-7fb876b2b34f427b/out/test_gl_bindings.rs
./target/debug/build/glutin-7fb876b2b34f427b/out/egl_bindings.rs
./target/debug/build/glutin-7fb876b2b34f427b/out/glx_extra_bindings.rs
./target/debug/build/gl-9653698dd50d604e/out/bindings.rs
./src/rust/level_serialization/mod.rs
./src/rust/map/mod.rs
./src/rust/player/mod.rs
./src/rust/lib.rs
./src/rust/main.rs
Perhaps cargo count
should either hardcode ignoring target/
, or read and ignore files ignored in .gitignore
like cargo does?
See for example this repo
I hacked at it but failed to make it work :v
$ rustc --version
rustc 1.52.1 (9bc8c42bb 2021-05-09)
$ sw_vers
ProductName: macOS
ProductVersion: 11.3.1
BuildVersion: 20E241
I'm seeing the following warnings when building with cargo build --release
:
Compiling libc v0.2.15
Compiling winapi-build v0.1.1
Compiling winapi v0.2.8
Compiling unicode-width v0.1.3
Compiling regex-syntax v0.3.5
Compiling unicode-segmentation v0.1.2
Compiling vec_map v0.6.0
Compiling bitflags v0.7.0
Compiling ansi_term v0.9.0
Compiling strsim v0.5.1
Compiling glob v0.2.11
Compiling utf8-ranges v0.1.3
Compiling tabwriter v0.1.25
Compiling kernel32-sys v0.2.2
Compiling memchr v0.1.11
Compiling term_size v0.2.1
Compiling gitignore v1.0.4
Compiling aho-corasick v0.5.2
Compiling clap v2.11.2
Compiling thread-id v2.0.0
Compiling thread_local v0.2.6
Compiling regex v0.1.75
Compiling cargo-count v0.2.4 (/Users/mjm/rust2/cargo-count)
warning: unused macro definition
--> src/macros.rs:17:1
|
17 | / macro_rules! werr(
18 | | ($($arg:tt)*) => ({
19 | | use std::io::{Write, stderr};
20 | | write!(&mut stderr(), $($arg)*).ok();
21 | | })
22 | | );
| |__^
|
= note: `#[warn(unused_macros)]` on by default
warning: unused macro definition
--> src/macros.rs:61:1
|
61 | / macro_rules! debug {
62 | | ($fmt:expr) => ();
63 | | ($fmt:expr, $($arg:tt)*) => ();
64 | | }
| |_^
warning: trait objects without an explicit `dyn` are deprecated
--> src/error.rs:58:32
|
58 | fn cause(&self) -> Option<&Error> {
| ^^^^^ help: use `dyn`: `dyn Error`
|
= note: `#[warn(bare_trait_objects)]` on by default
warning: use of deprecated trait `std::ascii::AsciiExt`: use inherent methods instead
--> src/config.rs:10:1
|
10 | / arg_enum! {
11 | | #[derive(Debug)]
12 | | pub enum Utf8Rule {
13 | | Ignore,
... |
16 | | }
17 | | }
| |_^
|
= note: `#[warn(deprecated)]` on by default
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
warning: use of deprecated associated function `std::error::Error::description`: use the Display impl or to_string()
--> src/macros.rs:6:54
|
6 | Err(e) => return Err(CliError::Generic(e.description().to_owned()))
| ^^^^^^^^^^^
|
::: src/main.rs:285:5
|
285 | cli_try!(counts.count());
| ------------------------- in this macro invocation
|
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
warning: use of deprecated associated function `std::error::Error::description`: use the Display impl or to_string()
--> src/macros.rs:6:54
|
6 | Err(e) => return Err(CliError::Generic(e.description().to_owned()))
| ^^^^^^^^^^^
|
::: src/main.rs:286:5
|
286 | cli_try!(counts.write_results());
| --------------------------------- in this macro invocation
|
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
warning: use of deprecated associated function `std::error::Error::description`: use the Display impl or to_string()
--> src/macros.rs:6:54
|
6 | Err(e) => return Err(CliError::Generic(e.description().to_owned()))
| ^^^^^^^^^^^
|
::: src/config.rs:55:34
|
55 | ret.push(cli_try!(env::current_dir()).join(p));
| ---------------------------- in this macro invocation
|
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
warning: use of deprecated associated function `std::error::Error::description`: use the Display impl or to_string()
--> src/macros.rs:6:54
|
6 | Err(e) => return Err(CliError::Generic(e.description().to_owned()))
| ^^^^^^^^^^^
|
::: src/config.rs:61:26
|
61 | ret.push(cli_try!(env::current_dir()).join(".git"));
| ---------------------------- in this macro invocation
|
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
warning: use of deprecated associated function `std::error::Error::description`: use the Display impl or to_string()
--> src/macros.rs:6:54
|
6 | Err(e) => return Err(CliError::Generic(e.description().to_owned()))
| ^^^^^^^^^^^
|
::: src/config.rs:65:22
|
65 | vec![cli_try!(env::current_dir()).join(".git")]
| ---------------------------- in this macro invocation
|
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
warning: use of deprecated associated function `std::error::Error::description`: use the Display impl or to_string()
--> src/macros.rs:6:54
|
6 | Err(e) => return Err(CliError::Generic(e.description().to_owned()))
| ^^^^^^^^^^^
|
::: src/config.rs:77:22
|
77 | vec![cli_try!(env::current_dir())]
| ---------------------------- in this macro invocation
|
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
warning: use of deprecated associated function `std::error::Error::description`: use the Display impl or to_string()
--> src/macros.rs:6:54
|
6 | Err(e) => return Err(CliError::Generic(e.description().to_owned()))
| ^^^^^^^^^^^
|
::: src/count/counts.rs:116:36
|
116 | let mut file_ref = cli_try!(File::open(&file));
| --------------------------- in this macro invocation
|
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
warning: use of deprecated associated function `std::error::Error::description`: use the Display impl or to_string()
--> src/macros.rs:6:54
|
6 | Err(e) => return Err(CliError::Generic(e.description().to_owned()))
| ^^^^^^^^^^^
|
::: src/count/counts.rs:126:25
|
126 | cli_try!(file_ref.read_to_end(&mut vec_buf));
| --------------------------------------------- in this macro invocation
|
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
warning: use of deprecated associated function `std::error::Error::description`: use the Display impl or to_string()
--> src/macros.rs:6:54
|
6 | Err(e) => return Err(CliError::Generic(e.description().to_owned()))
| ^^^^^^^^^^^
|
::: src/count/counts.rs:130:25
|
130 | cli_try!(file_ref.read_to_string(&mut buffer));
| ----------------------------------------------- in this macro invocation
|
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
warning: use of deprecated associated function `std::error::Error::description`: use the Display impl or to_string()
--> src/macros.rs:6:54
|
6 | Err(e) => return Err(CliError::Generic(e.description().to_owned()))
| ^^^^^^^^^^^
|
::: src/count/counts.rs:272:9
|
272 | / cli_try!(write!(w,
273 | | "\tLanguage\tFiles\tLines\tBlanks\tComments\tCode{}\n",
274 | | if self.cfg.usafe { "\tUnsafe (%)" } else { "" }));
| |___________________________________________________________________________- in this macro invocation
|
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
warning: use of deprecated associated function `std::error::Error::description`: use the Display impl or to_string()
--> src/macros.rs:6:54
|
6 | Err(e) => return Err(CliError::Generic(e.description().to_owned()))
| ^^^^^^^^^^^
|
::: src/count/counts.rs:275:9
|
275 | / cli_try!(write!(w,
276 | | "\t--------\t-----\t-----\t------\t--------\t----{}\n",
277 | | if self.cfg.usafe { "\t----------" } else { "" }));
| |___________________________________________________________________________- in this macro invocation
|
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
warning: use of deprecated associated function `std::error::Error::description`: use the Display impl or to_string()
--> src/macros.rs:6:54
|
6 | Err(e) => return Err(CliError::Generic(e.description().to_owned()))
| ^^^^^^^^^^^
|
::: src/count/counts.rs:285:17
|
285 | / cli_try!(write!(w,
286 | | "\t{}\t{}\t{}\t{}\t{}\t{}\t{}\n",
287 | | count.lang.name(),
288 | | count.total_files(),
... |
296 | | format!("{} ({:.2}%)", count.usafe(), usafe_per)
297 | | }));
| |____________________________________- in this macro invocation
|
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
warning: use of deprecated associated function `std::error::Error::description`: use the Display impl or to_string()
--> src/macros.rs:6:54
|
6 | Err(e) => return Err(CliError::Generic(e.description().to_owned()))
| ^^^^^^^^^^^
|
::: src/count/counts.rs:299:17
|
299 | cli_try!(write!(w, "\t{}\n", count));
| ------------------------------------- in this macro invocation
|
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
warning: use of deprecated associated function `std::error::Error::description`: use the Display impl or to_string()
--> src/macros.rs:6:54
|
6 | Err(e) => return Err(CliError::Generic(e.description().to_owned()))
| ^^^^^^^^^^^
|
::: src/count/counts.rs:302:9
|
302 | / cli_try!(write!(w,
303 | | "\t--------\t-----\t-----\t------\t--------\t----{}\n",
304 | | if self.cfg.usafe { "\t----------" } else { "" }));
| |___________________________________________________________________________- in this macro invocation
|
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
warning: use of deprecated associated function `std::error::Error::description`: use the Display impl or to_string()
--> src/macros.rs:6:54
|
6 | Err(e) => return Err(CliError::Generic(e.description().to_owned()))
| ^^^^^^^^^^^
|
::: src/count/counts.rs:305:9
|
305 | / cli_try!(write!(w,
306 | | "{}\t\t{}\t{}\t{}\t{}\t{}{}\n",
307 | | "Totals:",
308 | | fmt::format_number(self.tot as u64, self.cfg.thousands),
... |
318 | | "".to_owned()
319 | | }));
| |____________________________- in this macro invocation
|
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
warning: use of deprecated associated function `std::error::Error::description`: use the Display impl or to_string()
--> src/macros.rs:6:54
|
6 | Err(e) => return Err(CliError::Generic(e.description().to_owned()))
| ^^^^^^^^^^^
|
::: src/count/counts.rs:321:9
|
321 | cli_try!(w.flush());
| -------------------- in this macro invocation
|
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
warning: use of deprecated associated function `std::error::Error::description`: use the Display impl or to_string()
--> src/error.rs:44:58
|
44 | write!(f, "{} {}", Format::Error("error:"), self.description())
| ^^^^^^^^^^^
warning: 21 warnings emitted
Finished release [optimized] target(s) in 19.15s
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.