Commit graph

1791 commits

Author SHA1 Message Date
Yacin Tmimi
bc46af9742 Retain trailing comments in module when using rustfmt::skip attribute
Resolves 5033

Trailing comments at the end of the root Module were removed because the
module span did not extend until the end of the file.

The root Module's span now encompasses the entire file, which ensures
that no comments are lost when using ``#![rustfmt::skip]``
2021-10-28 21:44:39 -05:00
Caleb Cartwright
757da8c617 Merge remote-tracking branch 'upstream/master' into another-subtree-sync 2021-10-24 13:45:19 -05:00
Martinez
599b2fd9c4
Add One option to group_imports (#4966)
* Add Together option to group_imports

* Rename option to One

* Rename files from Together to One
2021-10-23 11:01:48 -05:00
Caleb Cartwright
606c7d49db ensure cargo-fmt tests are excluded from root workspace 2021-10-20 10:12:22 -05:00
Caleb Cartwright
2b41b6d022 Merge commit 'efa8f5521d3813cc897ba29ea0ef98c7aef66bb6' into rustfmt-subtree 2021-10-20 00:11:59 -05:00
Caleb Cartwright
923126348d
Merge branch 'master' into 1.4.38-subtree 2021-10-19 23:01:25 -05:00
Yacin Tmimi
5f4811ed7b Handle DefinitiveListTactic::SpecialMacro when writing pre-comments
Resolves 4615

Previously only Vertical and Mixed enum variants of DefinitiveListTactic
were considered when rewriting pre-comments for inner items in
lists::write_list.

Because we failed to considering the SpecialMacro variant we ended up in
a scenario where a ListItem with a pre_comment and a pre_comment_style
of ListItemCommentStyle::DifferentLine was written on the same line as the
list item itself.

Now we apply the same pre-comment formatting to SpecialMacro, Vertical,
and Mixed variants of DefinitiveListTactic.
2021-10-17 16:27:05 -05:00
Yacin Tmimi
f2fb3c9659 Update connector search in ControlFlow::rewrite_pat_expr for for loops
Resolves 5009

For loops represented by a ControlFlow object use " in" as their connector.
rustfmt searches for the first uncommented occurrence of the word "in" within the
current span and adjusts it's starting point to look for comments right after that.
visually this looks like this:

    rustfmt starts looking for comments here
            |
            V
    for x in /* ... */ 0..1 {}

This works well in most cases, however when the pattern also contains
the word "in", this leads to issues.

    rustfmt starts looking for comments here
          |
          V
    for in_here in /* ... */ 0..1 {}
        -------
        pattern

In order to correctly identify the connector, the new approach first
updates the span to start after the pattern and then searches for the
first uncommented occurrence of "in".
2021-10-13 19:36:37 -05:00
Yacin Tmimi
f7c4a44149 Adjust non-empty tuple struct span to start before fields
Resolves 5011

Tuple structs with visibility modifiers and comments before the first
field were incorrectly formatted. Comments would duplicate part of the
visibility modifier and struct name.

When trying to parse the tuple fields the ``items::Context`` searches
for the opening '(', but because the visibility modifier introduces
another '(' -- for example ``pub(crate)`` -- the parsing gets messed up.

Now the span is adjusted to start after the struct identifier, or after
any generics. Adjusting the span in this way ensures that the
``items::Contex`` will correctly find the tuple fields.
2021-10-12 20:08:07 -05:00
Yacin Tmimi
d41805704d Prevent structs with ".." from being rewritten with alignment
rustfmt should only support rewriting a struct in an expression
 position with alignment (non-default behavior) when there is no rest
 (with or without a base) and all of the fields are non-shorthand.
2021-10-12 19:37:48 -05:00
Yacin Tmimi
365a2f8f6e Add additional test cases for issue 4984 2021-10-05 22:29:23 -05:00
Yacin Tmimi
40f4993c67 Update derive attibute span to start after opening '('
Fixes 4984

When parsing derive attributes we're only concerned about the traits
and comments listed between the opening and closing parentheses.

Derive attribute spans currently start at the '#'.

    Span starts here
    |
    v
    #[derive(...)]

After this update the derive spans start after the opening '('.

    Span starts here
             |
             V
    #[derive(...)]
2021-10-05 22:29:23 -05:00
Patrick Walton
f0f449d6ed Wrap long array and slice patterns.
Closes #4530.
2021-10-04 20:41:17 -05:00
Ulyssa
e3203ef5e6 Add tests for binop_separator = Back 2021-09-28 22:20:34 -05:00
Yacin Tmimi
a5138b34d5 Prevent removal of qualified path for tuple struct inside macro
fixes 5005

This was very similar to 4964 and the fix was to extract and pass along
the qself of the ``PatKind::TupleStruct``
2021-09-27 17:49:10 -05:00
Caleb Cartwright
7f6229b9aa tests: restructure and extend cargo-fmt tests 2021-09-23 21:12:57 -05:00
Ulyssa
dd445aba08 Trailing comma on match block goes missing when guard is on its own line 2021-09-20 20:05:52 -05:00
Arjen Laarhoven
9d65b7dcd1 feat: upper- or lowercase hexadecimal literals 2021-09-14 21:22:26 -05:00
Seiichi Uchida
d4ffd1efa4 Support @generated marker to skip code formatting
This is a copy of #4296 with these changes:
* file is not reopened again to find if the file is generated
* first five lines are scanned for `@generated` marker instead of one
* no attempt is made to only search for marker in comments

`@generated` marker is used by certain tools to understand that the
file is generated, so it should be treated differently than a file
written by a human:
* linters should not be invoked on these files,
* diffs in these files are less important,
* and these files should not be reformatted.

This PR proposes builtin support for `@generated` marker.

I have not found a standard for a generated file marker, but:
* Facebook [uses `@generated` marker](https://tinyurl.com/fb-generated)
* Phabricator tool which was spawned from Facebook internal tool
  [also understands `@generated` marker](https://git.io/JnVHa)
* Cargo inserts `@generated` marker into [generated Cargo.lock files](https://git.io/JnVHP)

My personal story is that rust-protobuf project which I maintain
was broken twice because of incompatibilities/bugs in rustfmt marker
handling: [one](https://github.com/stepancheg/rust-protobuf/issues/493),
[two](https://github.com/stepancheg/rust-protobuf/issues/551).
(Also, rust-protobuf started generating `@generated` marker
[6 years ago](https://git.io/JnV5h)).

While rustfmt AST markers are useful to apply to a certain AST
elements, disable whole-file-at-once all-tools-at-once text level
marker might be easier to use and more reliable for generated code.
2021-09-14 21:22:26 -05:00
Nipunn Koorapati
b10ab51fed rustfmt doc code blocks with multiple comma-separated attributes
Added test covering this. Chose to treat the code block
as rust if and only if all of the comma-separated attributes
are rust-valid. Chose to allow/preserve whitespace around commas

Fixes #3158
2021-09-08 23:27:51 -05:00
Caleb Cartwright
a80688329c fix: handle param doc comments for macro scenarios 2021-09-08 19:27:31 -05:00
Caleb Cartwright
57548aa096 fix: resolve idempotency issue in extern body elements 2021-09-07 20:22:09 -05:00
Caleb Cartwright
3d8cd57c2f tests: add files for issue 4579 2021-09-06 18:14:13 -05:00
Caleb Cartwright
a59cac29f4 fix: use correct spans for params with attrs 2021-09-06 18:14:13 -05:00
Caleb Cartwright
59063e8b40 fix: newline width calc in combine w/ comments (#4123) 2021-09-06 17:02:20 -05:00
Eric
ae5696a7c4 test: add test for #4322 2021-09-02 20:53:15 -05:00
Eric
bfc60466bd test: add test for #4954 2021-09-02 20:53:15 -05:00
Eric
33d1368674 test: add test for #4257 2021-09-02 20:53:15 -05:00
Eric
d19f69cd71 test: add missing source for #4943 2021-09-02 20:53:15 -05:00
Yusuke Tanaka
ca9b050bbf Implement One option for imports_granularity (#4669)
This option merges all imports into a single `use` statement as long as
they have the same visibility.
2021-08-31 18:33:06 -05:00
Cameron Steffen
a603756cc5 Temporary fix rustfmt for let-else 2021-08-30 20:18:41 -05:00
Deadbeef
20ddab3def Fix rustfmt test 2021-08-27 11:53:03 +00:00
Deadbeef
076916fe94 Introduce ~const
- [x] Removed `?const` and change uses of `?const`
 - [x] Added `~const` to the AST. It is gated behind const_trait_impl.
 - [x] Validate `~const` in ast_validation.
 - [ ] Add enum `BoundConstness` to the HIR. (With variants `NotConst` and
 `ConstIfConst` allowing future extensions)
 - [ ] Adjust trait selection and pre-existing code to use `BoundConstness`.
 - [ ] Optional steps (*for this PR, obviously*)
      - [ ] Fix #88155
      - [ ] Do something with constness bounds in chalk
2021-08-27 05:07:37 +00:00
Ashvin Arsakularatne
e81c393663 fix: remove wrong reformatting of qualified paths in struct patterns 2021-08-25 21:18:41 -05:00
David Tolnay
fd6b025e8a Preserve visibility on trait items inside trait and impl 2021-08-22 12:06:18 -05:00
David Tolnay
c8bd550c8d Add test for visibility on a trait item (currently wrong) 2021-08-22 12:06:18 -05:00
Frank Steffahn
9bc0dbeb64 Fix typos “an”→“a” and a few different ones that appeared in the same search 2021-08-22 18:15:49 +02:00
Caleb Cartwright
5d8eb8d79c fix: don't drop drop generic args on assoc ty constraints 2021-08-08 13:58:10 -05:00
Caleb Cartwright
fefb5427a2 fix: handle GAT types in impls with self bounds 2021-08-06 22:03:40 -05:00
Ellen
0b21ea2161 Unyeet const param defaults 2021-07-27 19:50:11 -05:00
Caleb Cartwright
e4b8714c09 Merge commit '4236289b75ee55c78538c749512cdbeea5e1c332' into update-rustfmt 2021-07-25 22:57:19 -05:00
Caleb Cartwright
b305d62e5b fix: correct arm leading pipe check (#4880)
In the event a pattern starts with a leading pipe
the pattern span will contain, and begin with, the pipe.

This updates the process to see if a match arm contains
a leading pipe by leveraging this recent(ish) change to
the patterns in the AST, and avoids an indexing bug that
occurs when a pattern starts with a non-ascii char in the
old implementation.
2021-07-25 22:53:32 -05:00
Caleb Cartwright
2608f2c63b fix(rustfmt): load nested out-of-line mods correctly 2021-06-17 22:35:19 -05:00
Caleb Cartwright
2a3635d5d1 tests: remove snippets with inner attrs on non-block expressions 2021-05-04 18:38:22 -05:00
ChinYing-Li
84ff0013c2 Add the case in duplicate issue (#4806) to the idempotent tests 2021-04-21 21:30:42 -05:00
ChinYing-Li
dac2423f3f Recognize when a block comment has been ended inside a string literal (#4312) 2021-04-21 21:30:42 -05:00
Joseph Birr-Pixton
58157bb4b7 Add some basic tests for chain_width use 2021-04-21 21:27:50 -05:00
Caleb Cartwright
a168d92f9a tests: augment heuristics tests 2021-04-21 21:27:50 -05:00
Caleb Cartwright
1bcc1f8df5 refactor: apply heuristic config changes in lib 2021-04-21 21:27:50 -05:00
David Bar-On
432e09e89f Add the use of rewrite_assign_rhs_with_comments to 1.x 2021-02-17 20:47:20 -06:00