-
Notifications
You must be signed in to change notification settings - Fork 231
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
Fix S109 FP: named arguments, constructor calls, single-value attributes #4737
Comments
We want to ignore literal numbers when used in places where the meaning of them is obvious. Literal numbers where the meaning is obscured or hidden or not obvious is what makes them "magic" This would mean things like using a literal number in a Magic numbers are magic in any programming language. That Java check seems equally problematic. I'd define a list of identifiers that can hold a literal, something like
Then when visiting the usage of each literal number usage in the analyzer see if its being used for a parameter that has a name matching a record in the allowed list or if it matches the plural of an allowed list item, or if its the only parameter and the function name is From + one of the allowed literals or its plural. Another facet not yet discussed was finding things like |
I have some suggestions: I would argue that magic numbers should be ignored when used in creating a static readonly: class NoMagicNumber
{
static readonly SomeClass ReadOnlyClass = new SomeClass(1, 2, 3); // Compliant
static readonly SomeClass ReadOnlyClassChaine = new SomeClass().Add(3); // Complaint
static readonly SomeClass ReadOnlyFactory = SomeClass.FromInt(1234); // Compliant
} When extension methods are used to create value types: class NoMagicNumber
{
void ExtensionMethods()
{
/* struct */ Percentage percentage = 34.5.Percent(); // Compliant
TimeSpan duration = 12.Seconds(); // Compliant
}
} When value types (as class NoMagicNumber
{
void ValueTypes()
{
var date = new DateOnly(2017, 06, 11); // Compliant
var duration = TimeSpan.FromMilliseconds(12.4); // Compliant
var later = DateTime.NowUtc.Add(new TimeSpan(4, 5, 2)); // Compliant
}
}
|
Another case that should be excluded: GetHashcode() class Hash
{
public int Value1 { get; }
public string Value2 { get; }
public override int GetHashCode()
{
var hash1 = value?.GetHashCode() ?? 0;
return Value1 + (hash2 * 1566083941); // Compliant
}
public static int GetHashCode(object obj)
{
var hash = 1;
foreach (var prop in obj.GetType().GetProperties())
{
hash += prop.GetValue(obj).GetHashCode();
hash *= 17; // Compliant
}
return hash;
}
} |
@andrei-epure-sonarsource the list in the OP doesnt meet the minimum need to reduce FP unless all of the items it mentions are included (attributes and parameters). Anything less than that scope and there probably isnt enough value to to enable the rule again. |
What about Protobuf code first serialization where you have to put [ProtoMember(number)] and we have to pollute all our model classes with pragma? |
@StingyJack - do you mean if we ignore all numbers inside attributes, the rule wouldn't be useful anymore?
@cotzo - if we'd ignore all attributes altogether, this issue would be solved. Especially if there's only one parameter OR if named parameters are used. [Internal] We should look at projects on Peach and see what's noisy categories we can emerge from there. |
@Corniel - good points!
This is doable at syntax level.
I would avoid adding semantic calls into this rule because it would be quite costly. I would try to keep the improvements just at syntax level, if possible. |
I agree. Also, a constructor only containing literals is a save guess, I think. |
@andrei-epure-sonarsource - i mean that all 5 bullet pointed items need to be implemented for this rule. There isnt much value for me in the first three, |
Hi Andrei, My name is Benjamin, and my team is very interested in the resolution of this issue for a project we are working on right now. We wish to rid the codebase of its SonarLint warnings, but the attribute false positives really make that impossible or senseless for S109. What is the likely timeline on this, if any? Warm Regards, |
@bcrudolp I've started working on it, however I'm not sure what your usecase is. |
Hi Andrei, We are concerned with the false positives induced by attribute values. We have many of those that are simply indices or allowable ranges for settings, etc. Thanks! |
yes the attribute cases in handled in: #5247 |
Hi
I feel that reporting array indexes as magic numbers is a false positive. |
Yeah, that code is a classic example of a magic number and why they aren't good to use. |
Array declarations seem false positives as well
|
@sharbhajan Can you explain why you think these are FP? From my point of view your code samples are the exact cases S109 is supposed to report about. But I had trouble with this rule myself and am very interested in hearing your perspective. |
Array index: Array declarations: |
Both of these examples are very clear "magic numbers" in the sense that it is hard to understand from just looking at the code what these numbers mean and why they have been chosen. I don't want to dismiss your concern, though and instead fully understand where you are coming from. So let me ask you this: When do you find S109 issues valuable? Can you give a prime example where you would say "I'm glad S109 is raised here"? |
@Tim-Pohlmann I would argue (again) that if those signatures where declared as static read-only, they are the solution for the magic numbers themselves. You could argue that |
It seems there is some confusion of context here. Indeed, S109 is a good, valuable rule and raised genuine issues in a legacy project that I inherited, analyzed and reviewed. In almost all other areas, I found it very helpful in code maintainability. My concerns are only for arrays indexes and declarations. Array index: Array declarations:
S109 is raised for both code blocks, that is why I posted here. |
var PngSignatures = new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }; does not raise S109 for me. If it does for you, please report it as an FP with a reproducer in our community forum. Regarding the other cases: I'll raise awareness with my team to get a few different point of views. |
As an output of this community post: https://community.sonarsource.com/t/s109-magic-numbers-on-attributes/15784/16
And verifying the implementation of the same rule in our Java analyzer: https://github.com/SonarSource/sonar-java/blob/7.1.0.26670/java-checks/src/main/java/org/sonar/java/checks/MagicNumberCheck.java
We want to ignore magic numbers when used in:
Order
)Count
,Size
,Length
,Order
for single-digit integer valuesAlso consider (maybe have another internal talk) to tolerate:
The text was updated successfully, but these errors were encountered: