Using empty functions as default parameters should not be critical

Hello folks!

I hope you are doing well.

I need you! I have this small (and amazing) example component:

import React from "react";

export default function Plop({ handler = () => {} }) {
  return <button onClick={handler}>Plop</button>;

SonarLint is angry at me and raises a critical issue about this rule:

I think using empty functions as default parameters is a special case for this rule and should not raise a critical issue.

I use:

  • IntelliJ IDEA 2021.1.2 (Ultimate Edition) Build #IU-211.7442.40
  • SonarLint

Cheers :kissing_heart:!


Hi @Siegfried

This is the rule coming from ESLint core (no-empty-function - Rules - ESLint - Pluggable JavaScript linter), so we don’t have ability to adjust it directly.

Still motivation for this rule is applicable even for default parameter value: block {} can be confused with an empty object.

So the suggestion to you would be to add comment, e.g. /* do nothing */ inside empty block. You could also argue if rule should raise critical issues, but as it is a code smell, having critical is ok to me.

1 Like

Oh ok! Thanks a lot @Lena!

Have a nice day!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.