SonarSource/sonar-dotnet

Add [CodeCopiedFromAttribute] support

martin-strecker-sonarsource opened this issue · 0 comments

As described in this working document, we want to add a CodeCopiedFromAttribute to our code base, to mark code snippets from well-known sources. https://docs.google.com/document/d/1WgxCWCFyKt2mI46U83fWkpea06NT-EmwWIa_XmvWLM0/edit

A first attempt was implemented here, but needed to be reverted/removed, because the coverage exclusion did not work.
#8947 (comment). I created a ticket about the broken coverage handling #9588.

This issue is about re-introducing the attribute and marking all existing code that was copied from Roslyn or StyleCop with the attribute instead. The attribute API can be found here:
https://github.com/SonarSource/sonar-dotnet/blob/f42578d120d51c4648ce51c1694573e9ee2c7808/analyzers/src/SonarAnalyzer.Common/Helpers/CodeCopiedFromAttribute.cs

/*
 * SonarAnalyzer for .NET
 * Copyright (C) 2015-2024 SonarSource SA
 * mailto: contact AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

namespace SonarAnalyzer.Helpers;

public enum TrustedCodeSource
{
    Roslyn,
    StyleCop
}

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface | AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Property,
    AllowMultiple = true, Inherited = false)]
public sealed class CodeCopiedFromAttribute : Attribute
{
    public CodeCopiedFromAttribute(TrustedCodeSource trustedCodeSource, string permanentUrl)
    {
        TrustedCodeSource = trustedCodeSource;
        PermanentUrl = permanentUrl;
    }

    public TrustedCodeSource TrustedCodeSource { get; }
    public string PermanentUrl { get; }
    public string Reasoning {  get; set; }
}