Skip to content
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

Custom Rule shows warning message on incorrect line number #5766

Closed
2 tasks done
prabindatta opened this issue Aug 22, 2024 · 1 comment
Closed
2 tasks done

Custom Rule shows warning message on incorrect line number #5766

prabindatta opened this issue Aug 22, 2024 · 1 comment
Labels
help Questions or user problems that require more explanation rather than code changes.

Comments

@prabindatta
Copy link

prabindatta commented Aug 22, 2024

New Issue Checklist

Bug Description

I have added a custom rule to check if developer has used booleanVariable == true or booleanVariable == false except when we have optional chain like optionalVariable?.booleanVariable == true or optionalVariable?.booleanVariable == false
Rules are working and I can see warning message is shown to developers except it always appear couple of lines above the actual expected line of code.

// This triggers a violation:
func checkBooleanValues() {
        // Check isUserLoggedIn
        if isUserLoggedIn == true { // This line should have the warning message
            print("User is logged in")
            handleLoggedInUser()
        } else {
            print("User is not logged in")
            handleLoggedOutUser()
        }
}

Mention the command or other SwiftLint integration method that caused the issue. Include stack traces or command output.

# Type a script or drag a script file from your workspace to insert its path.
SWIFTLINT=${PODS_ROOT}/SwiftLint/swiftlint

if [ -f $SWIFTLINT ];
then
    "$SWIFTLINT"
else
    echo "warning: `swiftlint` command not found - See https://github.com/realm/SwiftLint#installation for installation instructions."
fi

Environment

SwiftLint version: 0.56.1
Xcode version: Xcode 15 (unrelated, replicable in CLI)
Installation method: Homebrew and Cocoapod (both have same behaviour)

  • Configuration file:
custom_rules:
  already_false:
    regex: '^(?:(?!\?).)* == false'
    message: "Don't compare to false, just use !value."

  already_true:
    regex: '^(?:(?!\?).)* == true'
    message: "Don't compare to true, just use the bool value."
@SimplyDanny
Copy link
Collaborator

This is mentioned in the README:

It is important to note that the regular expression pattern is used with the flags s and m enabled, that is . matches newlines and ^/$ match the start and end of lines, respectively. If you do not want to have . match newlines, for example, the regex can be prepended by (?-s).

Your regex matches up to as many lines before as it can. Since the position where it triggers is the beginning of the match be default, you see the warning "somewhere". Prepending the regex with (?-s) at least makes the rule trigger in the line where the violation actually appears.

@SimplyDanny SimplyDanny added the help Questions or user problems that require more explanation rather than code changes. label Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help Questions or user problems that require more explanation rather than code changes.
Projects
None yet
Development

No branches or pull requests

2 participants