Skip to content

don't reformat with errors unless --force flag supplied #4256

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

Merged
merged 1 commit into from
Jun 23, 2020

Conversation

calebcartwright
Copy link
Member

Closes #3989

@calebcartwright calebcartwright added this to the 2.0.0 milestone Jun 14, 2020
Copy link
Contributor

@topecongiro topecongiro left a comment

Choose a reason for hiding this comment

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

Thank you for the PR!

I would like to add a command-line flag that would ignore errors in general and format inputs, rather than enabling/disabling each error with command line arguments.

  • By default, rustfmt does not format inputs if there were formatting/operation errors
  • When -f,--force command line flag is passed, rustfmt formats inputs regardless of formatting errors

Note that what we treat as an error depends on the configuration:

  • By default DeprecatedAttr and BadAttr are the only errors that should be considered as a formatting error
  • If the license_template_path is used, above plus LicenseCheck
  • If the error_on_line_overflow is used, above plus LineOverflow
  • If the error_on_unformatted is used, above plus TrailingWhitespace
  • As MacroFormatError is internal, it should be ignored in any case

@calebcartwright
Copy link
Member Author

I would like to add a command-line flag that would ignore errors in general and format inputs, rather than enabling/disabling each error with command line arguments.

Ah okay. I was looking at #3989 only in the context of bad attributes, but the holistic error handling you've articulated sounds great.

I'll update this accordingly

@calebcartwright calebcartwright changed the title don't reformat with invalid rustfmt attributes by default don't reformat with errors unless --force flag supplied Jun 18, 2020
@calebcartwright
Copy link
Member Author

@topecongiro this should be ready for review whenever you get a chance. I only have two outstanding thoughts/questions:

  1. This incorporates the --force behavior and flag for both file and stdin inputs. Is this desired? I made them the same because I wanted to avoid differing behaviors between them
  2. Should we also include a --forceflag on cargo fmt that's a pass through to rustfmt (like we do for --check)?

@@ -413,6 +416,22 @@ fn format_string(input: String, opt: Opt) -> Result<i32> {
verbosity: Verbosity::Quiet,
};
let report = rustfmt_nightly::format(Input::Text(input), &config, setting)?;

if report.has_errors() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that we don't want to emit errors in stdin mode by default, as it clutters the output.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fair enough, though wouldn't that result in rustfmt silently exiting with an error code in such circumstances?

Copy link
Contributor

Choose a reason for hiding this comment

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

My bad, what I meant was when running in force mode, we do not want to emit the error report because both error reports and the formatting result will be printed.

To summarize the behavior of the stdin mode when executed in conjunction with multiple flags,

rustfmt # Print error message
rustfmt --quiet # No error message
rustfmt --verbose # Print error message
rustfmt --force # No error message
rustfmt --force --quiet # No error message
rustfmt --force --verbose # Print error message

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah okay, that makes sense!

Copy link
Contributor

@topecongiro topecongiro left a comment

Choose a reason for hiding this comment

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

Thank you so much for the updates; feels great to review the PR with a bunch of tests 😄

I commented on whether or not rustfmt should print the error message, but after reviewing the existing code, it seems that we don't take the flags like --quiet and --verbose into account properly in the first place.

I think we should sort out the spec of these flags on each mode and fix them in a different PR rather than rushing the fix. As such, this PR LGTM as is.

@calebcartwright calebcartwright merged commit 21eb88c into rust-lang:master Jun 23, 2020
@calebcartwright calebcartwright deleted the err-invalid-attrs branch June 23, 2020 23:19
@calebcartwright calebcartwright restored the err-invalid-attrs branch July 27, 2020 15:15
@calebcartwright calebcartwright deleted the err-invalid-attrs branch July 27, 2020 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Do not reformat on input error
3 participants