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

Sonar: Unnecessary use of conditional expression for default assignment #25097

Closed
qmonmert opened this issue Feb 5, 2024 · 4 comments · Fixed by #25098
Closed

Sonar: Unnecessary use of conditional expression for default assignment #25097

qmonmert opened this issue Feb 5, 2024 · 4 comments · Fixed by #25098
Assignees
Milestone

Comments

@qmonmert
Copy link
Contributor

qmonmert commented Feb 5, 2024

https://sonarcloud.io/project/issues?resolved=false&sinceLeakPeriod=true&types=CODE_SMELL&id=jhipster-sample-application&open=AY1qKP6qZqAZOZp0Vj8m

image

These two solutions don't work:
return { predicate: sortParam };
or
return { predicate: sortParam ?? undefined };

Indeed this test is in error:

    it('should accept empty string', () => {
      const sortState = service.parseSortParam('');
      expect(sortState).toEqual({});
    });

A solution which fixes sonar is:
return { predicate: (sortParam && sortParam !== '') ? sortParam : undefined };
but sortParam !== '' is useless.

Maybe add a NOSONAR ...

WDYT @mshima ?

Test:
image

@mshima
Copy link
Member

mshima commented Feb 5, 2024

return { predicate: sortParam || undefined };?

@qmonmert
Copy link
Contributor Author

qmonmert commented Feb 5, 2024

nope 🙂
image

@mshima
Copy link
Member

mshima commented Feb 5, 2024

return { predicate: Boolean(sortParam) ? sortParam : undefined };
return { predicate: sortParam?.length ? sortParam : undefined };
return { predicate: sortParam !== undefined && sortParam.length > 0 ? sortParam : undefined };
return { predicate: (sortParam ?? '').length > 0 ? sortParam : undefined };

if (sortParam) {
  return { predicate: sortParam };
}
return {};

@qmonmert
Copy link
Contributor Author

qmonmert commented Feb 5, 2024

return { predicate: sortParam?.length ? sortParam : undefined }; seems good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants