{"id":13478666,"url":"https://github.com/rossmacarthur/complexity","last_synced_at":"2025-04-13T16:08:49.003Z","repository":{"id":57609637,"uuid":"256943002","full_name":"rossmacarthur/complexity","owner":"rossmacarthur","description":"Calculate cognitive complexity of Rust code","archived":false,"fork":false,"pushed_at":"2021-02-20T10:03:30.000Z","size":50,"stargazers_count":17,"open_issues_count":2,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-13T16:08:43.582Z","etag":null,"topics":["cognitive-complexity","complexity","lint","syntax"],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rossmacarthur.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-19T07:46:37.000Z","updated_at":"2025-01-19T17:25:50.000Z","dependencies_parsed_at":"2022-08-27T11:32:32.667Z","dependency_job_id":null,"html_url":"https://github.com/rossmacarthur/complexity","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rossmacarthur%2Fcomplexity","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rossmacarthur%2Fcomplexity/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rossmacarthur%2Fcomplexity/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rossmacarthur%2Fcomplexity/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rossmacarthur","download_url":"https://codeload.github.com/rossmacarthur/complexity/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248741205,"owners_count":21154255,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cognitive-complexity","complexity","lint","syntax"],"created_at":"2024-07-31T16:02:00.253Z","updated_at":"2025-04-13T16:08:48.983Z","avatar_url":"https://github.com/rossmacarthur.png","language":"Rust","readme":"\u003ch1 align=\"center\"\u003ecomplexity\u003c/h1\u003e\n\u003cdiv align=\"center\"\u003e\n  \u003cstrong\u003eCalculate cognitive complexity of Rust code.\u003c/strong\u003e\n\u003c/div\u003e\n\u003cbr /\u003e\n\u003cdiv align=\"center\"\u003e\n  \u003ca href=\"https://crates.io/crates/complexity\"\u003e\n    \u003cimg src=\"https://img.shields.io/crates/v/complexity.svg\" alt=\"Crates.io version\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://docs.rs/complexity\"\u003e\n    \u003cimg src=\"https://docs.rs/complexity/badge.svg\" alt=\"Documentation\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/rossmacarthur/complexity/actions?query=workflow%3Abuild\"\u003e\n    \u003cimg src=\"https://img.shields.io/github/workflow/status/rossmacarthur/complexity/build/master\" alt=\"Build status\" /\u003e\n  \u003c/a\u003e\n\u003c/div\u003e\n\u003cbr\u003e\n\nBased on [Cognitive Complexity][pdf] by G. Ann Campbell.\n\n## Getting started\n\nAdd `complexity` to your `Cargo.toml`.\n\n```\n[dependencies]\ncomplexity = \"0.2\"\nsyn = \"1\"\n```\n\nYou'll need to bring the [`Complexity`] trait into scope, and probably some\nthings from [`syn`].\n\n```rust\nuse complexity::Complexity;\nuse syn::{Expr, parse_quote};\n```\n\nComplexity of expressions and other [`syn`] types is as simple as calling\n[`.complexity()`] on an instance of that type.\n\n```rust\nlet expr: Expr = parse_quote! {\n    for element in iterable { // +1\n        if something {        // +2 (nesting = 1)\n            do_something();\n        }\n    }\n};\nassert_eq!(expr.complexity(), 3);\n```\n\n## Examples\n\nThe implementation of cognitive complexity in this crate is heavily based on\n[Cognitive Complexity][pdf] by G. Ann Campbell. And reading it would be\nbeneficial to understanding how the complexity index is calculated.\n\nLoops and structures that introduce branching increment the complexity by one\neach. Some syntax structures introduce a \"nesting\" level which increases some\nexpressions complexity by that nesting level in addition to their regular\nincrement. In the example below we see how two nested loops and an if statement\ncan produce quite a high complexity of **7**.\n\n```rust\nuse complexity::Complexity;\nuse syn::{ItemFn, parse_quote};\n\nlet func: ItemFn = parse_quote! {\n    fn sum_of_primes(max: u64) -\u003e u64 {\n        let mut total = 0;\n        'outer: for i in 1..=max {   // +1\n            for j in 2..i {          // +2 (nesting = 1)\n                if i % j == 0 {      // +3 (nesting = 2)\n                    continue 'outer; // +1\n                }\n            }\n            total += i;\n        }\n    }\n};\nassert_eq!(func.complexity(), 7);\n```\n\nBut some structures are rewarded. Particularly a `match` statement, which only\nincreases the complexity by one no matter how many branches there are. (It does\nincrease the nesting level though.) In the example below we see how even though\nthere are a lot of branches in the code (which would contribute a lot to a more\ntraditional *cyclomatic complexity* measurement), the complexity is quite low at\n**1**.\n\n```rust\nuse complexity::Complexity;\nuse syn::{ItemFn, parse_quote};\n\nlet func: ItemFn = parse_quote! {\n    fn get_words(number: u64) -\u003e \u0026str {\n        match number {       // +1\n            1 =\u003e \"one\",\n            2 =\u003e \"a couple\",\n            3 =\u003e \"a few\",\n            _ =\u003e \"lots\",\n        }\n    }\n};\nassert_eq!(func.complexity(), 1);\n```\n\nAn example is provided to calculate and nicely print out the cognitive\ncomplexity of each function and method in an entire Rust file. See\n[examples/lint-files.rs](examples/lint-files.rs). You can run it on Rust files\nlike this:\n\n```sh\ncargo run --example lint-files -- src/\n```\n\n[pdf]: https://www.sonarsource.com/docs/CognitiveComplexity.pdf\n[`Complexity`]: https://docs.rs/complexity/0.2/complexity/trait.Complexity.html\n[`.complexity()`]: https://docs.rs/complexity/0.2/complexity/trait.Complexity.html#tymethod.complexity\n[`syn`]: https://docs.rs/syn/1\n\n## License\n\nLicensed under either of\n\n- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or\n   http://www.apache.org/licenses/LICENSE-2.0)\n- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)\n\nat your option.\n","funding_links":[],"categories":["Rust"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frossmacarthur%2Fcomplexity","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frossmacarthur%2Fcomplexity","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frossmacarthur%2Fcomplexity/lists"}