Add a rule to detect `toString()` calls on optional values
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 52/100
Research direction
Start by reading the existing core rules and their tests to learn how this analyzer checks expression types and reports diagnostics. Add a rule for toString() calls where the expression's static type includes (), and include tests for optional and non-optional values. The issue gives the expected warning and suggested nil handling; done means the optional case is reported while ordinary toString() calls are not.
Written by the indexing model from the issue text.
Description
Problem
Calling toString() on an optional value turns () into an empty string. The fact that the value was missing is lost, and nothing tells you:
string? middleName = ();
string name = middleName.toString(); // ""
The result looks like valid data, so it ends up in logs, payloads, and database rows as an empty string, and is hard to trace back later.
Proposed Solution
Add a core rule that reports a toString() call on an expression whose static type includes ().
Suggest handling the nil case explicitly, either with a default through the Elvis operator, or by returning an error when absence is a failure:
string name = middleName ?: "";
Interpolating an optional value into a string template is already a compile error, so only the toString() case needs a rule.
Alternatives
None that catch it at compile time; the conversion is valid and well defined.
Version
0.13.0
- Dominant language
- Java
- Stars
- 15
- Forks
- 12
- Avg merge
- 11h 57m
- Merged PRs (30d)
- 14
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- No contributing 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.
More from ballerina-platform/static-code-analysis-tool
-
Type/Improvement
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
ballerina-platform/static-code-analysis-tool#140 ·
Maintainers usually reply within 1 day
-
Type/Improvement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
ballerina-platform/static-code-analysis-tool#139 ·
Maintainers usually reply within 1 day
-
Type/Improvement
Difficulty 3/5 1-2 days Newbie friendliness 62/100
ballerina-platform/static-code-analysis-tool#138 ·
Maintainers usually reply within 1 day
-
Type/Improvement
Difficulty 3/5 1-2 days Newbie friendliness 63/100
ballerina-platform/static-code-analysis-tool#137 ·
Maintainers usually reply within 1 day
-
Type/Improvement
Difficulty 3/5 1-2 days Newbie friendliness 62/100
ballerina-platform/static-code-analysis-tool#136 ·
Maintainers usually reply within 1 day
All issues in ballerina-platform/static-code-analysis-tool
Similar issues
-
bug web-server
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
jooby-project/jooby#4035 ·
Maintainers usually reply within 1 day
-
L: javascript
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
dependabot/dependabot-core#16744 ·
Maintainers usually reply within 1 day
-
intake mcp-intake needs-human-review priority:high type:bug
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Ikalus1988/MisakaNet#3987 · 3 comments ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
quarkiverse/quarkus-google-cloud-services#1187 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 82/100
locationtech/jts#1250 ·