Movatterモバイル変換


[0]ホーム

URL:


CodeQL documentation
CodeQL resources

Useless call to GetHashCode()

ID: cs/useless-gethashcode-callKind: problemSecurity severity: Severity: recommendationPrecision: highTags:   - quality   - maintainability   - readability   - useless-codeQuery suites:   - csharp-security-and-quality.qls

Click to see the query in the CodeQL repository

The methodGetHashCode() on an integer simply returns the original value of the integer. This method call is therefore redundant, inefficient, and obscures the logic of the hash function. Several of the built-in types have this behavior, includingint,uint,short,ushort,long,ulong,byte andsbyte.

Recommendation

Remove the call toGetHashCode(), and review the hash function.

Example

The following hash function has two problems. Firstly, the calls toGetHashCode() are redundant, and secondly, the hash function generates too many collisions.

publicoverrideintGetHashCode(){returnrow.GetHashCode()^col.GetHashCode();}

These problems are resolved by removing the redundant calls toGetHashCode(), and by changing the hash function to generate fewer collisions.

publicoverrideintGetHashCode(){returnunchecked(row*16777619+col);}

References


[8]ページ先頭

©2009-2025 Movatter.jp