Check function calling inside functions using `custom_rules`
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 19.7k
- Forks
- 2.3k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 11
Description
New Issue Checklist
- Updated SwiftLint to the latest version
- I searched for existing GitHub issues
Describe the bug
I use custom_rule to show warnings for all @IBAction functions which don't call handleActionButton function inside these functions and it doesn't work correctly.
In that case, SwiftLint doesn't work correctly.
Here is the custom rule:
custom_rules:
button_action_tracking:
name: "Button Action Tracking"
regex: "@IBAction\\s+func\\s.*\\s(?!.*handleButtonAction)\\s"
message: "Don't forget to call `handleButtonAction` inside the method"
severity: warning
Input:
class Test {
@IBOutlet weak var button1: UIButton!
@IBOutlet weak var button2: UIButton!
@IBOutlet weak var button3: UIButton!
func handleButtonAction(with button: UIButton) {
}
@IBAction func button1Tapped() {
handleButtonAction(with: button2)
}
@IBAction func button2Tapped() {
}
@IBAction func button3Tapped() {
}
}
Linting:
$ swiftlint lint --path file1.swift
Loading configuration from '.swiftlint.yml'
Linting Swift files at path file1.swift
Linting 'file.swift' (1/1)
file1.swift:10:5: warning: Button Action Tracking Violation: Don't forget to call `handleButtonAction` inside the method (button_action_tracking)
file1.swift:20:1: warning: Trailing Newline Violation: Files should have a single trailing newline. (trailing_newline)
file1.swift:2:1: warning: Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
Done linting! Found 3 violations, 0 serious in 1 file.
However, SwiftLint highlights the first function instead of the second and third.
Expected behavior
https://regex101.com/r/TdkiNC/32

Environment
- SwiftLint version (run
swiftlint versionto be sure)?
0.42.0 - Installation method used (Homebrew, CocoaPods, building from source, etc)?
CocoaPods - Configuration file:
excluded:
- Pods
- vendor
disabled_rules:
- line_length
- identifier_name
- function_parameter_count
force_cast: warning
force_try: warning
cyclomatic_complexity:
error: 40
type_body_length:
error: 1000
function_body_length:
error: 200
file_length:
error: 1600
custom_rules:
button_action_tracking:
name: "Button Action Tracking"
regex: "@IBAction\\s+func\\s.*\\s(?!.*handleButtonAction)\\s"
message: "Don't forget to call `handleButtonAction` inside the method"
severity: warning
- Are you using nested configurations?
No - Which Xcode version are you using (check
xcodebuild -version)?
12.3 (12C33)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the warning placement with the supplied Swift input, custom_rules configuration, and swiftlint lint --path file1.swift command. Investigate how custom-rule regex matches function bodies and verify the result against the expected behavior: warnings should identify the @IBAction functions that do not call handleButtonAction.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100