Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

Add a rule to detect `toString()` calls on optional values

Open
#125 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
52/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
java
Domain
tooling

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

Type/New Feature
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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ballerina-platform/static-code-analysis-tool

All issues in ballerina-platform/static-code-analysis-tool

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.