New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New rule S1133: Deprecated code should be removed #6669
New rule S1133: Deprecated code should be removed #6669
Conversation
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/RemoveObsoleteCode.vb
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just added a couple of comments for more testcases
|
||
protected override string MessageFormat => "Do not forget to remove this deprecated code someday."; | ||
|
||
protected RemoveObsoleteCodeBase() : base(DiagnosticId) { } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a big overlap to ObsoleteAttributesNeedExplanation:
sonar-dotnet/analyzers/src/SonarAnalyzer.Common/Rules/ObsoleteAttributesNeedExplanationBase.cs
Lines 30 to 44 in c117567
protected ObsoleteAttributesNeedExplanationBase() : base(DiagnosticId) { } | |
protected sealed override void Initialize(SonarAnalysisContext context) => | |
context.RegisterNodeAction( | |
Language.GeneratedCodeRecognizer, | |
c => | |
{ | |
if (c.SemanticModel.GetSymbolInfo(c.Node).Symbol is { } attribute | |
&& attribute.IsInType(KnownType.System_ObsoleteAttribute) | |
&& !attribute.GetParameters().Any()) | |
{ | |
c.ReportIssue(Diagnostic.Create(Rule, c.Node.GetLocation())); | |
} | |
}, | |
Language.SyntaxKind.Attribute); |
What should we do @andrei-epure-sonarsource
- Merge this PR into master and do follow-up to merge the two rules
- Merge the two rules as part of this PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you mean merging the implementation of the rules, I'd suggest to do it in a separate PR
if you mean to merge the rules themselves, it should be a discussion at LT level as these are already implemented in other languages, too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant merging the implementations. I created #6698 to track the work.
What about the following case? [Obsolete("Will be dropped when the next major version is released.")]
public class Will_be_dropped
{
[Test]
public void TestSomething() => //..
} Tests that are marked as obsolete are in most (if not all) cases decorated as such because the call deprecated code, as a way to prevent CS0618. Do we want to ignore those? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, really extensive tests!
(please don't wait for a review from me to merge it, I don't have capacity to review it all and don't want to block it ; I was just wandering around the PRs previously when I left the comment) |
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/RemoveObsoleteCode.vb
Outdated
Show resolved
Hide resolved
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/RemoveObsoleteCode.vb
Outdated
Show resolved
Hide resolved
3b58f38
to
6922f42
Compare
I don't think we want to make any exceptions to the rule. The rule is to enable users in SonarCloud/SonarQube to track obsolete members. CS0618 doesn't raise when obsolete code calls obsolete code. The equivalent for this rule would be obsolete code scoped within obsolete code (e.g. obsolete method inside an obsolete class). I would still raise at both declarations because they might have a different deprecation policy. The scope for the rule is "All" and this includes tests. Tests that call obsolete code will be removed along with the obsolete member itself. So I don't see why we shouldn't track the obsolete test class. |
Kudos, SonarCloud Quality Gate passed! |
Kudos, SonarCloud Quality Gate passed! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, amazing testcase coverage.
Overly nitpick suggestion, feel free to ignore it and merge, I already approved:
I saw you added a record
example for versions >= C#10
, maybe add these as well:
[Obsolete]
record class RecordClass { }
[Obsolete]
record struct RecordStruct { }
[Obsolete]
ref struct RefStruct // still no idea what this does
{
[Obsolete]
public RefStruct() { }
[Obsolete]
ref int MeaningOfLife = 42; // C# 11 weirdness
}
@gregory-paidis-sonarsource This doesn't add much value with respect to the current implementation. When I did |
Because you will have two warnings (S1133 and CS0618) if you have one test on your obsolete code, which is noise. You do not have the risk of not removing the test, as by removing the obsolete test, your test will not longer compile. More generic, you could argue that a method that is marked as |
Fixes #6656
RSpec SonarSource/rspec#1512