Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 083a20f

Browse files
committed
version-gate the formatting of commented strings
1 parent baa62c6 commit 083a20f

File tree

7 files changed

+44
-8
lines changed

7 files changed

+44
-8
lines changed

src/utils.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use syntax::source_map::{BytePos, Span, NO_EXPANSION};
2222
use syntax_pos::Mark;
2323

2424
use comment::{filter_normal_code, CharClasses, FullCodeCharKind, LineClasses};
25-
use config::Config;
25+
use config::{Config, Version};
2626
use rewrite::RewriteContext;
2727
use shape::{Indent, Shape};
2828

@@ -527,8 +527,9 @@ pub fn trim_left_preserve_layout(orig: &str, indent: Indent, config: &Config) ->
527527
Some(get_prefix_space_width(config, &line))
528528
};
529529

530-
let new_veto_trim_value =
531-
(kind.is_string() || kind.is_commented_string()) && !line.ends_with('\\');
530+
let new_veto_trim_value = (kind.is_string()
531+
|| (config.version() == Version::Two && kind.is_commented_string()))
532+
&& !line.ends_with('\\');
532533
let line = if veto_trim || new_veto_trim_value {
533534
veto_trim = new_veto_trim_value;
534535
trimmed = false;
@@ -541,10 +542,12 @@ pub fn trim_left_preserve_layout(orig: &str, indent: Indent, config: &Config) ->
541542
// Because there is a veto against trimming and indenting lines within a string,
542543
// such lines should not be taken into account when computing the minimum.
543544
match kind {
544-
FullCodeCharKind::InString
545-
| FullCodeCharKind::EndString
546-
| FullCodeCharKind::InStringCommented
547-
| FullCodeCharKind::EndStringCommented => None,
545+
FullCodeCharKind::InStringCommented | FullCodeCharKind::EndStringCommented
546+
if config.version() == Version::Two =>
547+
{
548+
None
549+
}
550+
FullCodeCharKind::InString | FullCodeCharKind::EndString => None,
548551
_ => prefix_space_width,
549552
}
550553
})

tests/target/issue-3270.rs renamed to tests/source/issue-3270/one.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// rustfmt-version: One
2+
13
pub fn main() {
24
/* let s = String::from(
35
"

tests/source/issue-3270.rs renamed to tests/source/issue-3270/two.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// rustfmt-version: Two
2+
13
pub fn main() {
24
/* let s = String::from(
35
"

tests/target/issue-3132.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// rustfmt-version: Two
2+
13
fn test() {
24
/*
35
a

tests/target/issue-3270/one.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// rustfmt-version: One
2+
3+
pub fn main() {
4+
/* let s = String::from(
5+
"
6+
hello
7+
world
8+
",
9+
); */
10+
11+
assert_eq!(s, "\nhello\nworld\n");
12+
}

tests/target/issue-3270/two.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// rustfmt-version: Two
2+
3+
pub fn main() {
4+
/* let s = String::from(
5+
"
6+
hello
7+
world
8+
",
9+
); */
10+
11+
assert_eq!(s, "\nhello\nworld\n");
12+
}

tests/target/issue-3270-wrap.rs renamed to tests/target/issue-3270/wrap.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// rustfmt-wrap_comments: true
2+
// rustfmt-version: Two
23

4+
// check that a line below max_width does not get over the limit when wrapping
5+
// it in a block comment
36
fn func() {
47
let x = 42;
58
/*
6-
let something = "one line line line line line line line line line line line line
9+
let something = "one line line line line line line line line line line line line line
710
two lines
811
three lines";
912
*/

0 commit comments

Comments
 (0)