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

src: replace FromJust() with Check() when possible #27162

Closed

Conversation

sam-github
Copy link
Contributor

@sam-github sam-github commented Apr 9, 2019

FromJust() is often used not for its return value, but for its
side-effects. In these cases, Check() exists, and is more clear as to
the intent. From its comment:

To be used, where the actual value of the Maybe is not needed like
Object::Set.

See: https://github.com/nodejs/node/pull/26929/files#r269256335


I'm not dead sure this is a good idea, because Check() doesn't exist on 11.x, but on the other hand, it's
a trivial method, we could probably backport the v8 patch for it to 11.x if it's absence was painful.

Check() is much more clear in its intent, IMO, than FromJust() called just for its side-effect.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. labels Apr 9, 2019
@sam-github sam-github requested review from bnoordhuis and addaleax and removed request for bnoordhuis April 9, 2019 22:26
@sam-github sam-github mentioned this pull request Apr 9, 2019
4 tasks
Copy link
Member

@apapirovski apapirovski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO a positive change.

@targos
Copy link
Member

targos commented Apr 10, 2019

00:29:00  Running C++ linter...
00:29:16  File "src/node.cc" does not use "Just"

Copy link
Member

@bnoordhuis bnoordhuis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RSLGTM

FromJust() is often used not for its return value, but for its
side-effects. In these cases, Check() exists, and is more clear as to
the intent. From its comment:

  To be used, where the actual value of the Maybe is not needed like
  Object::Set.

See: https://github.com/nodejs/node/pull/26929/files#r269256335
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@ZYSzys ZYSzys added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Apr 12, 2019
@nodejs nodejs deleted a comment from nodejs-github-bot Apr 12, 2019
@sam-github
Copy link
Contributor Author

Landed in 060d901

@sam-github sam-github closed this Apr 12, 2019
sam-github added a commit that referenced this pull request Apr 12, 2019
FromJust() is often used not for its return value, but for its
side-effects. In these cases, Check() exists, and is more clear as to
the intent. From its comment:

  To be used, where the actual value of the Maybe is not needed, like
  Object::Set.

See: https://github.com/nodejs/node/pull/26929/files#r269256335

PR-URL: #27162
Reviewed-By: Anatoli Papirovski <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Gus Caplan <[email protected]>
Reviewed-By: Yongsheng Zhang <[email protected]>
@sam-github sam-github deleted the check-instead-of-fromjust branch April 12, 2019 19:35
@sam-github
Copy link
Contributor Author

Is it possible to make cpplint or the compiler warn (in just our src/) if the return value from FromJust() is not used? I looked at cpplint, it seems no. V8 has a macro V8_WARN_UNUSED_RESULT, perhaps some day they will decorate FromJust() with it, but it is not at the moment. Are there other options?

addaleax added a commit to addaleax/node that referenced this pull request Apr 15, 2019
Handle possible JS exceptions that can occur by returning to JS land
immediately.

The motivation for this change is that `USE(….FromJust());` is an
anti-pattern, and `.FromJust()` with an unused return value is
superseded by `.Check()`. However, in this case, checking that the
operation succeeded is not necessary.

Refs: nodejs#27162
addaleax added a commit that referenced this pull request Apr 17, 2019
Handle possible JS exceptions that can occur by returning to JS land
immediately.

The motivation for this change is that `USE(….FromJust());` is an
anti-pattern, and `.FromJust()` with an unused return value is
superseded by `.Check()`. However, in this case, checking that the
operation succeeded is not necessary.

Refs: #27162

PR-URL: #27245
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: James M Snell <[email protected]>
@bnoordhuis
Copy link
Member

V8 has a macro V8_WARN_UNUSED_RESULT, perhaps some day they will decorate FromJust() with it, but it is not at the moment.

@sam-github You could file a V8 CL that does that. Seems like a good change to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants