Skip to content

Issue #15985: Expand Default Value of constantWaiverParentToken in MagicNumberCheck with All Operators#15957

Merged
nrmancuso merged 1 commit intocheckstyle:masterfrom
checkstyle-GSoC25:extend-allow-list
Dec 9, 2024
Merged

Issue #15985: Expand Default Value of constantWaiverParentToken in MagicNumberCheck with All Operators#15957
nrmancuso merged 1 commit intocheckstyle:masterfrom
checkstyle-GSoC25:extend-allow-list

Conversation

@mahfouz72
Copy link
Copy Markdown
Member

@mahfouz72 mahfouz72 commented Nov 23, 2024

closes #15985

@mahfouz72
Copy link
Copy Markdown
Member Author

Github, generate report for MagicNumber/Example1

@github-actions
Copy link
Copy Markdown
Contributor

Failed to download or process the specified configuration(s).
Error details:

Please ensure you've used one of the following commands correctly:

@romani
Copy link
Copy Markdown
Member

romani commented Nov 23, 2024

Github, generate report for MagicNumber/Example1

@github-actions
Copy link
Copy Markdown
Contributor

@romani
Copy link
Copy Markdown
Member

romani commented Nov 24, 2024

@mahfouz72, please try to trigger report one more time, not clear why your execution failed.

@mahfouz72
Copy link
Copy Markdown
Member Author

Github, generate report for MagicNumber/Example1

@github-actions
Copy link
Copy Markdown
Contributor

@romani
Copy link
Copy Markdown
Member

romani commented Nov 24, 2024

https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/2cad2ada63b12f7151ee812317c057227694c7e2_2024105158/reports/diff/Vavr/index.html#A1

@Test
public void shouldCheckHashCodeInLeafList() {
    HashArrayMappedTrie<Integer, Integer> trie = empty();
    trie = trie.put(0, 1).put(null, 2);  // LeafList.hash == 0
    final Option<Integer> none = trie.get(1 << 6);  // (key.hash & BUCKET_BITS) == 0
    assertThat(none).isEqualTo(Option.none());
}

I am not sure that removal of this violation is good. but in the same time if there would be * instead of <<, there will be no violation, so making << so special is not very good.


https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/2cad2ada63b12f7151ee812317c057227694c7e2_2024105158/reports/diff/spring-framework/index.html#A1

/**
 * Base kind of the base reference types. The BASE_VALUE of such types is an
 * index into the type table.
 */
static final int OBJECT = BASE | 0x700000;

kind of magic number, but it is field, so should be ignored if ignoreFieldDeclaration=true, but this report is for default config, so it is false.


https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/2cad2ada63b12f7151ee812317c057227694c7e2_2024105158/reports/diff/spring-framework/xref/home/runner/work/checkstyle/checkstyle/.ci-temp/repositories/spring-framework/spring-core/src/main/java/org/springframework/asm/Type.java.html#L104

/**
 * The <tt>void</tt> type.
 */
public static final Type VOID_TYPE = new Type(VOID, null, ('V' << 24)
        | (5 << 16) | (0 << 8) | 0, 1);

it will be kind of impossible to persuade users to put all this numbers in separate variables/constants, as 5 in one expression and in another expression can means different. And naming all digits is too much.

@mahfouz72
Copy link
Copy Markdown
Member Author

kind of magic number, but it is field, so should be ignored if ignoreFieldDeclaration=true, but this report is for default config,

This is ignored because the field is constant, the same as we ignore / (BASE / 5, for example), so | has nothing special to be a violation.

I agree with extending the allow list to make the check less strict. Users can still customize the list if needed.

please let me know your final decision.

Copy link
Copy Markdown
Member

@romani romani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know what other operators are not this waiver?
I just try to formulate some reasons of this property and tokens presence in it.
To make it clear what is default and what users should do themselves.

Items:

ignoreAnnotation = (default)false
ignoreFieldDeclaration = true
ignoreAnnotationElementDefaults = (default)true
constantWaiverParentToken = (default)TYPECAST, METHOD_CALL, EXPR, ARRAY_INIT, UNARY_MINUS, \
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We changed default, but we didn't change all other Inputs with "(default)".
Please do.
I am not sure in what issue we will do validation of default, I will investigate later on.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#15983 is created to improve separately.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mahfouz72 , please update other Inputs with default on this property to have such tokens.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be a huge update we have many inputs for this check :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need it, I hope it is just copy paste

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. It was painful because I had to change row numbers in every unit test

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, we always keep 2 empty lines in config comment to avoid line shift.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The addition is 2 lines, and I can't remove the 2 empty lines in the config comment because there will be no empty lines at all and we have a test for this.

It will be good if we had 3 empty lines not 2

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, next time try to reuse empty line in header comment, if post.

@romani
Copy link
Copy Markdown
Member

romani commented Nov 28, 2024

Can we suggest user in issue a workaround by setting of this property with required tokens?

@mahfouz72
Copy link
Copy Markdown
Member Author

Do you know what other operators are not this waiver?

There are a bunch of other operators that are not on the list such as mod operation, comparison operators, and conditional operators

Example:

D:\CS\test
cat src/Test.java
public class Test {

    public static final boolean BIT6 = 1 >= 3; // violation
    public static final boolean BIT7 = 1 != 3; // violation
    public static final int BIT8 = 1 % 3; // violation
    public static final int BIT9 = 1 > 2 ? 6 : 5; // 2 violations
}
D:\CS\test
java  -jar checkstyle-10.17.0-all.jar -c config.xml src/Test.java
Starting audit...
[ERROR] D:\CS\test\src\Test.java:3:45: '3' is a magic number. [MagicNumber]
[ERROR] D:\CS\test\src\Test.java:4:45: '3' is a magic number. [MagicNumber]
[ERROR] D:\CS\test\src\Test.java:5:40: '3' is a magic number. [MagicNumber]
[ERROR] D:\CS\test\src\Test.java:6:44: '6' is a magic number. [MagicNumber]
[ERROR] D:\CS\test\src\Test.java:6:48: '5' is a magic number. [MagicNumber]
Audit done.
Checkstyle ends with 5 errors.

I just try to formulate some reasons of this property and tokens presence in it.
To make it clear what is default and what users should do themselves.

I still can't formulate reasons why we add some operators and not others. I don't think of any use case when we could violate magic numbers with one operator but not others.

The thing here is consistency. If one operator triggers a magic number violation, why should others (like addition or subtraction) not? Can we say that some operators (like arithmetic ones) are more commonly used with numbers That is why we added them in default list but others are not commonly used so we leave the option for users to add them to the list.

@romani
Copy link
Copy Markdown
Member

romani commented Dec 1, 2024

We can make any rule we think is reasonable. Let's drop few ideas/reasons and choose by vote what is more clear to active maintainers.

I did big post at #15950 (comment) to investigate problem.

@mahfouz72
Copy link
Copy Markdown
Member Author

We can make any rule we think is reasonable. Let's drop few ideas/reasons and choose by vote what is more clear to active maintainers.

moved to #15985

@mahfouz72 mahfouz72 closed this Dec 2, 2024
@mahfouz72 mahfouz72 reopened this Dec 7, 2024
@mahfouz72 mahfouz72 changed the title Issue #15950: Add bitwise operators to list of allowed tokens in Magi… Issue #15985: Expand Default Value of constantWaiverParentToken in MagicNumberCheck with All Operators Dec 7, 2024
@romani
Copy link
Copy Markdown
Member

romani commented Dec 7, 2024

@mahfouz72 , see #15957 (comment)

…oken in MagicNumberCheck with All Operators
Copy link
Copy Markdown
Member

@romani romani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok to merge

Copy link
Copy Markdown
Contributor

@nrmancuso nrmancuso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mahfouz72 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expand Default Value of constantWaiverParentToken in MagicNumberCheck with All Operators

3 participants