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

Print entire comment line for report_todo/report_fixme #3912

Merged
merged 1 commit into from
Nov 14, 2019

Conversation

tangmi
Copy link
Contributor

@tangmi tangmi commented Nov 8, 2019

This changeset make it so the entire comment line is printed when report_todo or report_fixme finds an issue.

It is motivated by the following example. Consider the following source:

// TODO implement something!
fn main() {}

Currently rustfmt will output something similar to:

warning: found TODO without issue number
  --> path/to/source/file.rs
   |
 1 | // TODO
   |

This change accumulates all the TODO and FIXME issues for each line and outputs the entire comment in the error message:

warning: found TODO without issue number
  --> path/to/source/file.rs
   |
 1 | // TODO implement something!
   |

Some concerns:

  • I'm not sure if CharClasses or files always end in a \n, or if we need to handle the "todo in a comment at the end of a file with no trailing newline" edge case explicitly.
  • I'm not sure how to test this effectively.

Thanks for your time!

@calebcartwright
Copy link
Member

Hi @tangmi!

I'm not sure how to test this effectively.

I'd suggest adding a test to src/test/mod.rs with something like the below snippet:

#[test]
fn todo_test() {
    init_log();
    // let filename = "tests/issue-checks/todo.rs";
    // let input = Input::File(filename.into());
    let source = "// TODO implement something!\nfn main() {}\n";
    let input = Input::Text(source.to_owned());
    let output = vec![
        "warning: found TODO",
        " --> stdin:1",
        "  |",
        "1 | // TODO",
        "  |\n",
        "warning: rustfmt has failed to format. See previous 1 errors.\n",
    ].join("\n");
    let mut config = Config::default();
    config.set().report_todo(ReportTactic::Always);
    config.set().emit_mode(EmitMode::Stdout);
    let mut buf: Vec<u8> = vec![];
    let mut session = Session::new(config, Some(&mut buf));
    let report = session.format(input).unwrap();
    let errors = ReportedErrors {
        has_check_errors: true,
        has_formatting_errors: true,
        ..Default::default()
    };
    assert_eq!(session.errors, errors);
    let actual = format!("{}", FormatReportFormatterBuilder::new(&report).build());
    assert_eq!(actual, output);
}

For the sake of simplicity I structured that example with everything inline, though you could move the input/output to separate files if you prefer (I left a commented out line for using a file input should you decide to do so). And of course you'd need to update that expected output to include the file comment line, replace stdin with the filename if you move the input to a file, etc.

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 your first contribution to rustfmt! The code LGTM.

That being said, however, report_todo and report_fixme are options that I am more inclined to remove from rustfmt. I feel that these options don't provide much value over the plain rg TODO or rg FIXME.

I will merge this PR because it looks good on its own, but please keep in mind that the options may get removed in the future.

@topecongiro topecongiro merged commit 3773466 into rust-lang:master Nov 14, 2019
@ytmimi
Copy link
Contributor

ytmimi commented Mar 31, 2022

Given that #5101 and #5102 plan to remove the report_todo and report_fixme I'm changing the label to backport:n/a

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.

5 participants