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

add support for --message-format option in cargo fmt #3752

Merged
merged 1 commit into from
Aug 27, 2019

Conversation

calebcartwright
Copy link
Member

Closes #3274

--message-format in the other cargo subcommands accepts 3 values:

  1. human
  2. short
  3. json

In the context of rustfmt, my thinking was:

  1. human -> basically a noop, defaults to the standard/regular output
  2. short -> list the names of files with diffs
  3. json -> maps to the json emit mode

IIRC, using --message-format short with clippy lists the file names along with the line numbers with lint violations, but I didn't think trying to include line numbers made as much sense for rustfmt in the short mode.

Each of these output/message formats is already available via other flags/options that have been recently added (--emit json, -l, --files-with-diff), so the implementation is basically just mapping the --message-format value to those corresponding args rustfmt already supports.

As examples:
short

$ cargo +nightly fmt --message-format short -- --check
src/lib.rs
src/foo.rs

json (pretty printed after for ease of view)

$ cargo +nightly fmt --message-format json
[  
  {  
    "name":"tests/writemode/source/json.rs",
    "mismatches":[  
      {  
        "original_begin_line":5,
        "original_end_line":7,
        "expected_begin_line":5,
        "expected_end_line":5,
        "original":"fn foo_expr() {\n    1\n}",
        "expected":"fn foo_expr() { 1 }"
      },
      {  
        "original_begin_line":9,
        "original_end_line":11,
        "expected_begin_line":7,
        "expected_end_line":7,
        "original":"fn foo_stmt() {\n    foo();\n}",
        "expected":"fn foo_stmt() { foo(); }"
      },
      {  
        "original_begin_line":13,
        "original_end_line":15,
        "expected_begin_line":9,
        "expected_end_line":9,
        "original":"fn foo_decl_local()  {\n    let z = 5;\n   }",
        "expected":"fn foo_decl_local() { let z = 5; }"
      },
      {  
        "original_begin_line":17,
        "original_end_line":19,
        "expected_begin_line":11,
        "expected_end_line":11,
        "original":"fn    foo_decl_item(x: &mut i32) {\n    x = 3;\n}",
        "expected":"fn foo_decl_item(x: &mut i32) { x = 3; }"
      },
      {  
        "original_begin_line":21,
        "original_end_line":21,
        "expected_begin_line":13,
        "expected_end_line":13,
        "original":"   fn empty()     {",
        "expected":"fn empty() {}"
      },
      {  
        "original_begin_line":23,
        "original_end_line":23,
        "expected_begin_line":15,
        "expected_end_line":15,
        "original":"}",
        "expected":"fn foo_return() -> String { \"yay\" }"
      },
      {  
        "original_begin_line":25,
        "original_end_line":29,
        "expected_begin_line":17,
        "expected_end_line":20,
        "original":"fn foo_return() -> String {\n    \"yay\"\n}\n\nfn foo_where() -> T where T: Sync {",
        "expected":"fn foo_where() -> T\nwhere\n    T: Sync,\n{"
      },
      {  
        "original_begin_line":64,
        "original_end_line":66,
        "expected_begin_line":55,
        "expected_end_line":55,
        "original":"fn lots_of_space                      ()                                                           {\n                           1                 \n}",
        "expected":"fn lots_of_space() { 1 }"
      },
      {  
        "original_begin_line":71,
        "original_end_line":72,
        "expected_begin_line":60,
        "expected_end_line":60,
        "original":"    fn dummy(&self) {\n    }",
        "expected":"    fn dummy(&self) {}"
      },
      {  
        "original_begin_line":75,
        "original_end_line":75,
        "expected_begin_line":63,
        "expected_end_line":64,
        "original":"trait CoolerTypes { fn dummy(&self) { ",
        "expected":"trait CoolerTypes {\n    fn dummy(&self) {}"
      },
      {  
        "original_begin_line":77,
        "original_end_line":77,
        "expected_begin_line":66,
        "expected_end_line":66,
        "original":"}",
        "expected":""
      },
      {  
        "original_begin_line":79,
        "original_end_line":79,
        "expected_begin_line":67,
        "expected_end_line":70,
        "original":"fn Foo<T>() where T: Bar {",
        "expected":"fn Foo<T>()\nwhere\n    T: Bar,\n{"
      }
    ]
  }
]

{
print_usage_to_stderr(&msg);
return FAILURE;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

I started out implementing everything inline here, but extracted it out into a separate function to make it easier to test

src/cargo-fmt/main.rs Outdated Show resolved Hide resolved
src/cargo-fmt/main.rs Outdated Show resolved Hide resolved
@calebcartwright calebcartwright force-pushed the message-format branch 2 times, most recently from 28ce76b to e9f8def Compare August 26, 2019 01:42
@topecongiro topecongiro merged commit c26c0e5 into rust-lang:master Aug 27, 2019
@topecongiro
Copy link
Contributor

Thanks!

@topecongiro topecongiro added this to the 1.4.6 milestone Aug 27, 2019
@calebcartwright calebcartwright deleted the message-format branch August 27, 2019 00:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cargo fmt doesn't accept --message-format.
2 participants