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

Don't lint 'unused_parens on if (break _) { .. }` #54885

Merged
merged 1 commit into from
Oct 30, 2018

Conversation

llogiq
Copy link
Contributor

@llogiq llogiq commented Oct 7, 2018

This fixes #54704

@rust-highfive
Copy link
Collaborator

r? @nikomatsakis

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 7, 2018
@rust-highfive

This comment has been minimized.

@rust-highfive

This comment has been minimized.

@rust-highfive

This comment has been minimized.

@rust-highfive

This comment has been minimized.

@rust-highfive

This comment has been minimized.

@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-llvm-5.0 of your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
[00:46:18] .................................................................................................... 2500/4572
[00:46:22] .................................................................................................... 2600/4572
[00:46:25] .................................................................................................... 2700/4572
[00:46:28] .................................................................................................... 2800/4572
[00:46:32] ..................................................................................F................. 2900/4572
[00:46:38] ................................................................................................i.i. 3100/4572
[00:46:41] .ii................................................................................................. 3200/4572
[00:46:44] .................................................................................................... 3300/4572
[00:46:47] ..........................................i......................................................... 3400/4572
---
[00:47:24] 
[00:47:24] ---- [ui] ui/lint/unused_parens_json_suggestion.rs stdout ----
[00:47:24] diff of stderr:
[00:47:24] 
[00:47:24] 8   "spans": [
[00:47:24] 9     {
[00:47:24] 10       "file_name": "$DIR/unused_parens_json_suggestion.rs",
[00:47:24] -       "byte_start": 1043,
[00:47:24] -       "byte_end": 1056,
[00:47:24] -       "line_start": 25,
[00:47:24] -       "line_end": 25,
[00:47:24] +       "byte_start": 1071,
[00:47:24] +       "byte_end": 1084,
[00:47:24] +       "line_start": 26,
[00:47:24] +       "line_end": 26,
[00:47:24] 15       "column_start": 14,
[00:47:24] 16       "column_end": 27,
[00:47:24] 17       "is_primary": true,
[00:47:24] 
[00:47:24] 66       "spans": [
[00:47:24] 67         {
[00:47:24] 68           "file_name": "$DIR/unused_parens_json_suggestion.rs",
[00:47:24] -           "byte_start": 1043,
[00:47:24] -           "byte_end": 1056,
[00:47:24] -           "line_start": 25,
[00:47:24] -           "line_end": 25,
[00:47:24] +           "byte_start": 1071,
[00:47:24] +           "byte_end": 1084,
[00:47:24] +           "line_start": 26,
[00:47:24] +           "line_end": 26,
[00:47:24] 73           "column_start": 14,
[00:47:24] 74           "column_end": 27,
[00:47:24] 75           "is_primary": true,
[00:47:24] 91     }
[00:47:24] 92   ],
[00:47:24] 92   ],
[00:47:24] 93   "rendered": "warning: unnecessary parentheses around assigned value
[00:47:24] -   --> $DIR/unused_parens_json_suggestion.rs:25:14
[00:47:24] +   --> $DIR/unused_parens_json_suggestion.rs:26:14
[00:47:24] 95    |
[00:47:24] 96 LL |     let _a = (1 / (2 + 3));
[00:47:24] 
[00:47:24] 
[00:47:24] The actual stderr differed from the expected stderr.
[00:47:24] Actual stderr saved to /checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/lint/unused_parens_json_suggestion/unused_parens_json_suggestion.stderr
[00:47:24] Actual stderr saved to /checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/lint/unused_parens_json_suggestion/unused_parens_json_suggestion.stderr
[00:47:24] To update references, rerun the tests and pass the `--bless` flag
[00:47:24] To only update this specific test, also pass `--test-args lint/unused_parens_json_suggestion.rs`
[00:47:24] error: 1 errors occurred comparing output.
[00:47:24] status: exit code: 0
[00:47:24] status: exit code: 0
[00:47:24] command: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/src/test/ui/lint/unused_parens_json_suggestion.rs" "--target=x86_6ability": null,
[00:47:24]       "expansion": null
[00:47:24]   ],
[00:47:24]   ],
[00:47:24]   "children": [
[00:47:24]     {
[00:47:24]       "message": "lint level defined here",
[00:47:24]       "code": null,
[00:47:24]       "level": "note",
[00:47:24]       "spans": [
[00:47:24]         {
[00:47:24]           "file_name": "/checkout/src/test/ui/lint/unused_parens_json_suggestion.rs",
[00:47:24]           "byte_start": 889,
[00:47:24]           "byte_end": 902,
[00:47:24]           "line_start": 20,
[00:47:24]           "line_end": 20,
[00:47:24]           "column_start": 9,
[00:47:24]           "column_end": 22,
[00:47:24]           "is_primary": true,
[00:47:24]           "text": [
[00:47:24]             {
[00:47:24]               "text": "#![warn(unused_parens)]",
[00:47:24]               "highlight_start": 9,
[00:47:24]               "highlight_end": 22
[00:47:24]           ],
[00:47:24]           ],
[00:47:24]           "label": null,
[00:47:24]           "suggested_replacement": null,
[00:47:24]           "suggestion_applicability": null,
[00:47:24]           "expansion": null
[00:47:24]       ],
[00:47:24]       ],
[00:47:24]       "children": [],
[00:47:24]       "rendered": null
[00:47:24]     {
[00:47:24]     {
[00:47:24]       "message": "remove these parentheses",
[00:47:24]       "code": null,
[00:47:24]       "level": "help",
[00:47:24]       "spans": [
[00:47:24]         {
[00:47:24]           "file_name": "/checkout/src/test/ui/lint/unused_parens_json_suggestion.rs",
[00:47:24]           "byte_start": 1071,
[00:47:24]           "byte_end": 1084,
[00:47:24]  .rs
[00:47:24] test result: FAILED. 4551 passed; 1 failed; 20 ignored; 0 measured; 0 filtered out
[00:47:24] 
[00:47:24] thread 'main' panicked at 'Some tests failed', tools/compiletest/src/main.rs:499:22
[00:47:24] 
[00:47:24] 
[00:47:24] 
[00:47:24] command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-tools-bin/compiletest" "--compile-lib-path" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/lib" "--run-lib-path" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/x86_64-unknown-linux-gnu/lib" "--rustc-path" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "--src-base" "/checkout/src/test/ui" "--build-base" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui" "--stage-id" "stage2-x86_64-unknown-linux-gnu" "--mode" "ui" "--target" "x86_64-unknown-linux-gnu" "--host" "x86_64-unknown-linux-gnu" "--llvm-filecheck" "/usr/lib/llvm-5.0/bin/FileCheck" "--host-rustcflags" "-Crpath -O -Zunstable-options " "--target-rustcflags" "-Crpath -O -Zunstable-options  -Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "--docck-python" "/usr/bin/python2.7" "--lldb-python" "/usr/bin/python2.7" "--gdb" "/usr/bin/gdb" "--quiet" "--llvm-version" "5.0.0\n" "--system-llvm" "--cc" "" "--cxx" "" "--cflags" "" "--llvm-components" "" "--llvm-cxxflags" "" "--adb-path" "adb" "--adb-test-dir" "/data/tmp/work" "--android-cross-path" "" "--color" "always"
[00:47:24] 
[00:47:24] 
[00:47:24] failed to run: /checkout/obj/build/bootstrap/debug/bootstrap test
[00:47:24] Build completed unsuccessfully in 0:03:23
[00:47:24] Build completed unsuccessfully in 0:03:23
[00:47:24] Makefile:58: recipe for target 'check' failed
[00:47:24] make: *** [check] Error 1

The command "stamp sh -x -c "$RUN_SCRIPT"" exited with 2.
travis_time:start:0a2afa44
$ date && (curl -fs --head https://google.com | grep ^Date: | sed 's/Date: //g' || true)
---
travis_time:end:258abd2c:start=1539086481341711806,finish=1539086481346082347,duration=4370541
travis_fold:end:after_failure.3
travis_fold:start:after_failure.4
travis_time:start:0d0d7a0b
$ ln -s . checkout && for CORE in obj/cores/core.*; do EXE=$(echo $CORE | sed 's|obj/cores/core\.[0-9]*\.!checkout!\(.*\)|\1|;y|!|/|'); if [ -f "$EXE" ]; then printf travis_fold":start:crashlog\n\033[31;1m%s\033[0m\n" "$CORE"; gdb --batch -q -c "$CORE" "$EXE" -iex 'set auto-load off' -iex 'dir src/' -iex 'set sysroot .' -ex bt -ex q; echo travis_fold":"end:crashlog; fi; done || true
travis_fold:end:after_failure.4
travis_fold:start:after_failure.5
travis_time:start:20dc4b58
travis_time:start:20dc4b58
$ cat ./obj/build/x86_64-unknown-linux-gnu/native/asan/build/lib/asan/clang_rt.asan-dynamic-i386.vers || true
cat: ./obj/build/x86_64-unknown-linux-gnu/native/asan/build/lib/asan/clang_rt.asan-dynamic-i386.vers: No such file or directory
travis_fold:end:after_failure.5
travis_fold:start:after_failure.6
travis_time:start:1c874a6a
$ dmesg | grep -i kill

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @TimNN. (Feature Requests)

@nikomatsakis
Copy link
Contributor

Travis is still pretty grumpy.

@llogiq
Copy link
Contributor Author

llogiq commented Oct 9, 2018

Sorry about that, I failed to commit the changed .stderr – will do that once I get to a PC with a stable connection.

@llogiq
Copy link
Contributor Author

llogiq commented Oct 10, 2018

Travis is now happy.

@kleimkuhler
Copy link
Contributor

This will likely introduce a merge conflict with #54820 just a heads up! I don't mind on the order these go in; I think both will be pretty easy to rebase from.

err.emit();
if struct_lit_needs_parens &&
(parser::contains_exterior_struct_lit(&inner) ||
if let ast::ExprKind::Break(..) = inner.node {
Copy link
Contributor

Choose a reason for hiding this comment

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

You can probably simplify this addition down to ... || inner.node == ast::ExprKind::Break

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That would be surprising to say the least, because ExprKind::Break is defined as a tuple struct of two values (note the two dots). I didn't know you could compare with incomplete value definitions.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry yes you may still need the two dots! What I was trying to point out is that you can just check if inner.node is a ExprKind::Break as the whole condition and get ride of the ... { true } else { false }.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How would that work? The if let needs some values in the then/else branches.

Copy link
Contributor

@kleimkuhler kleimkuhler Oct 10, 2018

Choose a reason for hiding this comment

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

Ah yes I see what you mean. I was under the impression we could get a bool from inner.node == ast::ExprKind::Break(..). What I was mainly hoping to simplify in all this was getting rid of the additional ... { true } else { false } as it just looks a little odd to read.

A way I see doing that is keeping things how they were (with necessary) and going for something like adding those additional four lines to check for a Break.

let necessary = struct_lit_needs_parens && parser::contains_exterior_struct_lit(&inner);
if !necessary {
+    // We don't want to lint when there is a `break` in `inner`
+    if let ast::ExprKind::Break(..) = inner.node {
+        return;
+    }
    ...
}

Either way - just an initial reaction I had to seeing the new additions! I'm fine keeping it how it is.

@llogiq
Copy link
Contributor Author

llogiq commented Oct 13, 2018

@nikomatsakis r?

@bors
Copy link
Contributor

bors commented Oct 15, 2018

☔ The latest upstream changes (presumably #55095) made this pull request unmergeable. Please resolve the merge conflicts.

Copy link
Contributor

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

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

r=me with nits below fixed

err.emit();
if struct_lit_needs_parens &&
(parser::contains_exterior_struct_lit(&inner) ||
if let ast::ExprKind::Break(..) = inner.node {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: I'd prefer to see this if let captured into a temporary value, or perhaps with a helper function. This is a pretty complex conditional! Maybe even break it into a if

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That'd be a pity; I just found a way to use a return instead of a boolean that would shorten the code considerably. But I can (and should) add a comment why we don't lint on break here.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't follow what would be a pity...? I was just suggesting reformatting things

Copy link
Contributor

Choose a reason for hiding this comment

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

ping @llogiq

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I've been busy lately. I'll revisit and rebase this later this week.

@@ -270,47 +270,52 @@ impl UnusedParens {
msg: &str,
struct_lit_needs_parens: bool) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: this name no longer seems very accurate; let's call it something like is_restricted_expr: bool perhaps? And I think a comment on check_unused_parens_core would be good. Something like:


Invoked on expressions that appear in places where people traditionally put parentheses out of habit, but where they are not needed in Rust. For example, if (a == b) { .. }. Issues a lint if value is a parenthesized node, unless those parentheses are deemed "necessary".

Parentheses are considered necessary if (a) is_restricted_expr is true, which indicates that this is a context (such as an if) where only a subset of expressions are permitted and (b) the parenthesized expression falls outside of that subset. So e.g. if (Foo { a: 22 }) { .. } would not get a warning; similarly while (break) { .. } would not.

@nikomatsakis nikomatsakis added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 23, 2018
@llogiq
Copy link
Contributor Author

llogiq commented Oct 29, 2018

Rebased on current master, this time I could piggy-back on the recent changes to the lint.

@nikomatsakis
Copy link
Contributor

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Oct 29, 2018

📌 Commit 1a37575 has been approved by nikomatsakis

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 29, 2018
kennytm added a commit to kennytm/rust that referenced this pull request Oct 30, 2018
Don't lint 'unused_parens` on `if (break _) { .. }`

This fixes rust-lang#54704
bors added a commit that referenced this pull request Oct 30, 2018
Rollup of 12 pull requests

Successful merges:

 - #54885 (Don't lint 'unused_parens` on `if (break _) { .. }`)
 - #55205 (Improve a few cases of collecting to an FxHash(Map/Set))
 - #55450 (msp430: remove the whole Atomic* API)
 - #55459 (Add UI test for #49296)
 - #55472 (Use opt.take() instead of mem::replace(opt, None))
 - #55473 (Take advantage of impl Iterator in (transitive/elaborate)_bounds)
 - #55474 (Fix validation false positive)
 - #55476 (Change a flat_map with 0/1-element vecs to a filter_map)
 - #55487 (Adjust Ids of path segments in visibility modifiers)
 - #55493 (Doc fixes)
 - #55494 (borrowck=migrate must look at parents of closures)
 - #55496 (Update clippy)

Failed merges:

r? @ghost
@bors bors merged commit 1a37575 into rust-lang:master Oct 30, 2018
@llogiq llogiq deleted the fix-54704 branch October 30, 2018 17:26
@kennytm kennytm mentioned this pull request Oct 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect suggestion to remove parentheses around if-condition containing break
5 participants