{"id":22493326,"url":"https://github.com/ufcpp/noncopyableanalyzer","last_synced_at":"2025-08-03T01:32:02.841Z","repository":{"id":27553751,"uuid":"113949486","full_name":"ufcpp/NonCopyableAnalyzer","owner":"ufcpp","description":"An analyzer for Non-Copyable structs.","archived":false,"fork":false,"pushed_at":"2022-06-23T01:46:36.000Z","size":146,"stargazers_count":31,"open_issues_count":7,"forks_count":4,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-12-04T17:17:04.543Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ufcpp.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"ufcpp"}},"created_at":"2017-12-12T05:54:51.000Z","updated_at":"2024-10-11T12:36:10.000Z","dependencies_parsed_at":"2022-07-27T09:49:00.567Z","dependency_job_id":null,"html_url":"https://github.com/ufcpp/NonCopyableAnalyzer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ufcpp%2FNonCopyableAnalyzer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ufcpp%2FNonCopyableAnalyzer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ufcpp%2FNonCopyableAnalyzer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ufcpp%2FNonCopyableAnalyzer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ufcpp","download_url":"https://codeload.github.com/ufcpp/NonCopyableAnalyzer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228514096,"owners_count":17932346,"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":[],"created_at":"2024-12-06T18:38:21.250Z","updated_at":"2024-12-06T18:38:21.982Z","avatar_url":"https://github.com/ufcpp.png","language":"C#","funding_links":["https://github.com/sponsors/ufcpp"],"categories":[],"sub_categories":[],"readme":"# NonCopyableAnalyzer\n\nSome kind of structs should not be copied (especially mutable structs).\nThis package contains an analyzer for such a struct.\n\nNuGet: [https://www.nuget.org/packages/NonCopyableAnalyzer/](https://www.nuget.org/packages/NonCopyableAnalyzer/)\n\n## Example\n\nA typical example of a non-copyable struct is [`ResizableArray`](https://github.com/dotnet/corefxlab/blob/master/src/System.Collections.Sequences/System/Collections/Sequences/ResizableArray.cs).\nSo I'll use it for explanation. Let's copy it from [corefxlab](https://github.com/dotnet/corefxlab/blob/master/src/System.Collections.Sequences/System/Collections/Sequences/ResizableArray.cs) and add an attribute named `NonCopyable`.\n\n```cs\nnamespace System.Collections.Sequences\n{\n    // copy from corefxlab\n    // a List\u003cT\u003e like type designed to be embeded in other types\n    // this kind of type should not be copied\n    [NonCopyable]\n    public struct ResizableArray\u003cT\u003e\n    {\n        ...\n    }\n```\n\nThe analyzer checks the attribute only by name - any namespace, both internal and public are OK.\n\n```cs\nusing System;\n\n[AttributeUsage(AttributeTargets.Struct)]\ninternal class NonCopyableAttribute : Attribute { }\n```\n\nNow, the analyzer reports misuse of the non-copyable struct.\n\n### Misuse 1: pass by value\n\n```cs\nusing System.Collections.Sequences;\n\nclass Program\n{\n    static void Main()\n    {\n        var a = new ResizableArray\u003cshort\u003e();\n\n        // do not pass a non-copyable by value\n        AddUtf16(a, \"abcd\");\n    }\n\n    static void AddUtf16(ResizableArray\u003cshort\u003e a, string s)\n    {\n        foreach (var c in s)\n        {\n            a.Add((short)c);\n        }\n    }\n}\n```\n\n![do not pass a non-copyable by value](docs/NonCopyablePassByValue.png)\n\n### Misuse 2: read-only\n\n```cs\nusing System.Collections.Sequences;\n\nclass Program\n{\n    static void Main()\n    {\n        var a = new ResizableArray\u003cshort\u003e();\n\n        AddUtf16(a, \"abcd\");\n    }\n\n    static void AddUtf16(in ResizableArray\u003cshort\u003e a, string s)\n    {\n        foreach (var c in s)\n        {\n            // do not invoke methods/properties with ref readonly\n            a.Add((short)c);\n        }\n    }\n}\n```\n\n![do not invoke methods/properties with ref readonly](docs/NonCopyableReadOnlyRef.png)\n\n### How to fix\n\nIn this case, you should use `ref` parameter.\n\n```cs\nusing System.Collections.Sequences;\n\nclass Program\n{\n    static void Main()\n    {\n        var a = new ResizableArray\u003cshort\u003e();\n\n        AddUtf16(ref a, \"abcd\");\n    }\n\n    static void AddUtf16(ref ResizableArray\u003cshort\u003e a, string s)\n    {\n        foreach (var c in s)\n        {\n            a.Add((short)c);\n        }\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fufcpp%2Fnoncopyableanalyzer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fufcpp%2Fnoncopyableanalyzer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fufcpp%2Fnoncopyableanalyzer/lists"}