Commit Graph

1322 Commits

Author SHA1 Message Date
Tom Milligan
c240f3a6b3
feat: add skip_macro_invocations option (#5347)
* feat: add skip_macro_names option

* [review] update configuration documentation

* [review] fix docstring

* [feat] implement wildcard macro invocation skip

* commit missed files

* [review] test override skip macro names

* [review] skip_macro_names -> skip_macro_invocations

* [review] expand doc configuration

* [review] add lots more tests

* [review] add use alias test examples

* [review] add link to standard macro behaviour
2022-07-12 19:31:19 -05:00
Maybe Waffle
f026688c2a Add rustfmt test for formatting for<> before closures 2022-07-12 21:00:13 +04:00
Yacin Tmimi
2403f827bf Add test case for issue 1306 which was resolved
Closes 1306

It's unclear when the issue was fixed, but it cannot be reproduced.
2022-07-09 19:59:09 -05:00
Caleb Cartwright
ac595dd57a Merge commit 'c4416f20dcaec5d93077f72470e83e150fb923b1' into sync-rustfmt 2022-06-22 22:14:32 -05:00
Yacin Tmimi
0156575a32 Revert "Memoize format_expr"
Fixes 5399

Memoizing expressions lead to cases where rustfmt's stability guarantees
were violated.

This reverts commit a37d3ab0e1.
2022-06-22 17:45:42 -05:00
sec65
33c60740d3
Add width for codeblocks in comments (#5372)
* add doc_comment_code_block_width configuration

* updated config docu

* Updated docu and changed tests to config folder
2022-06-16 22:15:16 -05:00
Yacin Tmimi
e44380b341 Version gate raw identifier use statement sorting
When useing `version=One` rustfmt will treat the leading `r#` as part of
the `UseSegment` used for sorting. When using `version=Two` rustfmt will
ignore the leading `r#` and only consider the name of the identifier
when sorting the `UseSegment`.
2022-06-15 20:06:04 -05:00
Yacin Tmimi
5ae94cc6b8 Backport 3795
fix sorting of use statements with raw identifiers
2022-06-15 20:06:04 -05:00
Caleb Cartwright
d03a547cad Merge commit '7b73b60faca71d01d900e49831fcb84553e93019' into sync-rustfmt 2022-06-12 22:03:05 -05:00
David Bar-On
7d34cfaf2c Dedup imports_granularity = "Item" (#4737)
* Fix for issue 4725 - dedup Item imports_granularity (2nd version)

* Use unique() instead of unique_by()
2022-06-11 19:57:47 -05:00
Caleb Cartwright
1ac2ac0db6 Merge remote-tracking branch 'upstream/master' into subtree-sync-2022-06-07 2022-06-07 22:06:28 -05:00
Caleb Cartwright
79515f17ed feat: remove license_template_path config option 2022-05-30 08:56:09 -05:00
Jacob Pratt
241a6f6930 Remove crate visibility modifier in libs, tests 2022-05-21 00:32:47 -04:00
ydah
86940d2652 Fix typo
This PR is fixes typo "avaiable" to "available".
2022-05-20 10:39:10 +09:00
David Lattimore
95837832f4 import_granularity: Don't normalize imports with comments 2022-05-18 21:08:30 -05:00
Pascal Seitz
c65ba14d69 Fixes #5260
Fixes #5260 by checking if it is part of a type '::'
2022-05-08 13:22:35 -05:00
hkalbasi
a37d3ab0e1 Memoize format_expr 2022-04-17 09:32:43 -05:00
Tom Milligan
acdab00ecc [review] check interaction with import grouping 2022-04-17 09:28:48 -05:00
Paul Gey
ba0351a146 Preserve attributes for imports_granularity=Item
Fixes #5030
2022-04-17 09:28:48 -05:00
Tom Milligan
4f3f87fb9a group_imports: test and document non-consecutive imports 2022-04-11 17:46:33 -05:00
Tharun Rajendran
1dcdfb276d
fix(rustfmt): fix struct field formatting with doc comments present (#5217)
* fix(rustfmt): fix struct field formatting with doc comments present

Fixes #5215

* fix review feedbacks

* add unit test without doc comment

* move tests to a seperate file

* add additional test cases

* reintroduce a newline at the of test/souce/structs.rs
2022-04-01 19:00:53 -05:00
Caleb Cartwright
8211d64c71 Merge commit '5ff7b632a95bac6955611d85040859128902c580' into sync-rustfmt-subtree 2022-03-29 23:17:30 -05:00
David Tolnay
5ff7b632a9 Preserve semicolon after macro call inside foreign mod 2022-03-29 22:20:11 -05:00
David Tolnay
8e94761a94 Add test of macro calls inside extern block 2022-03-29 22:20:11 -05:00
Yacin Tmimi
8984438a6f Honor #[rustfmt::skip::attributes(derive)] attribute
Fixes 5270

Previously, rustfmt only checked the `merge_derives` configuration value
to determine if it should merge_derives. This lead to derives being
merged even when annotated with the `rustfmt::skip` attribute.

Now, rustfmt also checks if derives are explicitly being skipped in the
current context via the `rustfmt::skip` attribute.
2022-03-21 16:56:34 -05:00
123vivekr
0dba01aee1 Add short_item_threshold config option
Allow custom short item threshold values via config
2022-03-21 16:56:09 -05:00
Tom Milligan
b4de150dbc fix: imports_granularity module with path containing self 2022-03-12 10:47:46 -06:00
cassaundra
ab9f2a8ac7 Add more tests for struct_field_align_threshold and trailing_comma 2022-03-06 11:23:50 -06:00
cassaundra
ee130515e3 Fix missing struct field separators under certain conditions
When struct_field_align_threshold is non-zero and trailing_comma is set to
"Never," struct field separators are omitted between field groups. This issue is
resolved by forcing separators between groups.

Fixes #4791.

A test is included with a minimal reproducible example.
2022-03-06 11:23:50 -06:00
Yacin Tmimi
272fb42f06 Prevent wrapping markdown headers in doc comments
Fixes 5238

A markdown header is defined by a string that starts with `#`.

Previously, rustfmt would wrap long markdown headers when
`wrap_comments=true`. This lead to issues when rendering these headers
in HTML using rustdoc.

Now, rustfmt leaves markdown headers alone when wrapping comments.
2022-02-28 23:25:49 -06:00
Yacin Tmimi
1e78a2b258 Leverage itemized blocks to support formatting markdown block quotes
Fixes 5157

Doc comments support markdown, but rustfmt didn't previously assign any
semantic value to leading '> ' in comments. This lead to poor formatting
when using ``wrap_comments=true``.

Now, rustfmt treats block quotes as itemized blocks, which greatly
improves how block quotes are formatted when ``wrap_comments=true``.
2022-02-11 17:31:06 -06:00
Gabriel Smith
ace7241087 Fix incorrect string indentation in macro defs with format_strings 2022-02-10 21:02:59 -06:00
Stéphane Campinas
fd6e11cc57 Add tests for the One and Crate variants 2022-02-03 18:56:53 -06:00
Stéphane Campinas
b2c7a52ea8 Fix import_granularity option when the use tree has an alias 2022-02-03 18:56:53 -06:00
Yacin Tmimi
606894eb0b Retain trailing separator when extracting the last inline post comment
Fixes 5042

Previously, trailing commas were removed from the last inline comment.
This lead to rustfmt refusing to format code snippets because
the original comment did not match the rewritten comment.

Now, when rustfmt extracts the last inline comment it leaves trailing
separators alone. Rustfmt does not need to remove these separators
because they are commented out.
2022-02-03 18:52:58 -06:00
Stéphane Campinas
368a9b7cef
Handle non-ascii character at boundary (#5089)
* Handle non-ascii character at boundary

* Replace substraction underflow check with early termination
2022-02-01 19:06:14 -06:00
Szymon Gibała
b4a4bf0bf8
Fix formatting of comments in empty structs (#5171)
* Fix formatting of comments in empty structs

* Add tests

* Add single line tests

* Fix block comments

* Revert changes of test source files
2022-01-28 22:55:47 -06:00
Caleb Cartwright
6db6bafc61 Merge commit '4a053f206fd6799a25823c307f7d7f9d897be118' into sync-rustfmt-subtree 2021-12-29 20:49:39 -06:00
David Lattimore
4a053f206f Do not flatten match arm block with leading attributes
This is a backport of #4124.

Fixes #4109
2021-12-29 17:43:58 -06:00
David Tolnay
0b2fd9b132 Fix static async closure qualifier order 2021-12-23 23:03:49 -06:00
Caleb Cartwright
0167c5303f Merge commit '8da837185714cefbb261e93e9846afb11c1dc60e' into sync-rustfmt-subtree 2021-12-02 21:35:30 -06:00
Yacin Tmimi
ec46ffd981 Determine when new comment lines are needed for itemized blocks
Fixes 5088

Previously, rustfmt would add a new comment line anytime it reformatted
an itemized block within a comment when ``wrap_comments=true``. This
would lead to rustfmt adding empty comments with trailing whitespace.

Now, new comment lines are only added if the original comment spanned
multiple lines, if the comment needs to be wrapped, or if the comment
originally started with an empty comment line.
2021-12-01 18:31:10 -06:00
mujpao
ea042b90c9 Add more tests for comments in lists 2021-11-27 11:38:36 -06:00
mujpao
196e676504 Preserve normalized comments after last list item 2021-11-19 19:18:17 -06:00
Caleb Cartwright
0023abfb2c tests: add cases for type alias issues 2021-11-18 13:43:37 -06:00
Caleb Cartwright
f99e3582bd Merge commit 'ea199bacef07213dbe008841b89c450e3bf0c638' into rustfmt-sync 2021-11-07 20:37:34 -06:00
mujpao
4d50e7c760 Put empty trait braces on same line if possible 2021-11-05 20:40:49 -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
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
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
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
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
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
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
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
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
David Bar-On
432e09e89f Add the use of rewrite_assign_rhs_with_comments to 1.x 2021-02-17 20:47:20 -06:00
David Bar-On
4b0ed96f2e Fix for issue 4603 about extra macro body indentation (third version) 2021-02-17 20:19:27 -06:00
Caleb Cartwright
d80a42fdd8 tests: add case for issue 4675 2021-01-28 22:01:50 -06:00
Caleb Cartwright
c13d2452c0 chore: backport some empty block check fixes 2021-01-27 20:58:42 -06:00
vallentin
1e2b0b7c72 Added 4646 test case 2021-01-27 20:58:42 -06:00
Michael Morgan
384ba68d64 Add imports_granularity="Item".
This option splits all imports into their own `use` statement.
2021-01-27 20:58:42 -06:00
Sean Klein
5e14f760c5 fix: Avoid incorrect global 'cfg_if' Symbol interning
Fixes #4656
2021-01-27 19:26:58 -06:00
topecongiro
152ccb5059 Do not duplicate const keyword on parameters 2021-01-27 18:50:03 -06:00
Seiichi Uchida
c0fede355c Use the span after generics and where clause (#4208) 2021-01-27 18:50:03 -06:00
hafiz
269584634a Include constness in impl blocks (#4215)
Closes #4084
2021-01-27 18:50:03 -06:00
Geoffry Song
71863753bd Rename merge_imports to imports_granularity and add a Module option.
This renames the existing `true`/`false` options to `Crate`/`Never`, then adds a
new `Module` option which causes imports to be grouped together by their
originating module.
2021-01-17 11:48:47 -06:00
Caleb Cartwright
b30cb24286 fix: indentation issue on generic bounds 2021-01-16 11:17:23 -06:00
Caleb Cartwright
3571c5d6f7 fix: maintain redundant semis on items in statement pos 2021-01-09 12:11:52 -06:00
Seiichi Uchida
0d022d08d8 Format error and typeof types (#4416)
* Add a test for #4357

* Format error and typeof types
2020-12-20 12:05:05 -06:00
Stéphane Campinas
c536d80dc1 Fix rewrite of closures with a return type
If the closure's body fits in a line, the block is removed but it is
necessary if the closure has a return type.
2020-12-20 12:05:05 -06:00
Caleb Cartwright
4cfb9ef8f4 fix: don't strip nonexistent comma in derive 2020-12-20 12:05:05 -06:00
David Tolnay
c40a8c1ecc Add negative impl test 2020-11-30 23:24:36 -06:00
WhizSid
7d9ee7558e Comment between typebounds (#4474)
* Test cases and get spans

* Fixed type bounds

* Fixed issue of test cases

* Fixed first test case issue

* Removed unwanted whitespaces

* Removed tmp files
2020-11-29 13:26:58 -06:00