Potential fix for code scanning alert no. 32: Complex condition#6
Merged
Conversation
Complex return condition simplified through variable assignment Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Potential fix for https://github.com/DoubledDoge/typeguard/security/code-scanning/32
In general, to fix complex conditions you extract parts of the logic into well-named boolean variables or helper methods, then combine those with a simpler expression. This preserves behavior while making the intent clearer and reducing the cognitive load when reading or modifying the code.
For this specific case in
src/TypeGuard.Core/Rules/IpAddressRules.cs, the best approach is to factor each private range check into its own local boolean variable insideIsValid, then return the disjunction (||) of those variables. For example, createbool is10Range,bool is172Range,bool is192Range, andbool isApipaRange, assigning them the same conditions currently embedded in the singlereturnstatement. Then change thereturntoreturn is10Range || is172Range || is192Range || isApipaRange;. This keeps all logic within the same method, avoids changing signatures or adding public helpers, and does not alter functionality. No new imports are required, and all changes are confined to the body ofPrivateIpRule.IsValid(lines 37–51).Suggested fixes powered by Copilot Autofix. Review carefully before merging.