Repository: DevinR528/cargo-sort (language: rust)

Issue:
Let `-c` or some new flag take formatting into consideration
We use `cargo-sort` in CI to check if entries are sorted but it would also be super helpful to validate if it has been formatted correctly. I suppose making `-c` return a non-zero exit code would be a breaking change, so perhaps some other option would be necessary.


Discussion / hints:
That would be nice, I'd be open to a PR adding the flag! Beware, the formatting feature is more a consequence of the toml looking terrible after sorting, so it may not be an exhaustive format check. 

Fix patch:
diff --git a/src/main.rs b/src/main.rs
index cabb8b8..993e3ac 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -72,11 +72,16 @@ fn check_toml(
     let mut sorted_str = sorted.to_string_in_original_order();
     let is_sorted = toml_raw == sorted_str;
 
-    // if no-format is not found apply formatting
-    if !matches.is_present("no-format") {
-        fmt::fmt_toml(&mut sorted, config);
-        sorted_str = sorted.to_string_in_original_order();
-    }
+    let is_formatted =
+        // if no-format is not found apply formatting
+        if !matches.is_present("no-format") || matches.is_present("check-format") {
+            let original = sorted_str.clone();
+            fmt::fmt_toml(&mut sorted, config);
+            sorted_str = sorted.to_string_in_original_order();
+            original == sorted_str
+        } else {
+            true
+        };
 
     if config.crlf && !sorted_str.contains("\r\n") {
         sorted_str = sorted_str.replace("\n", "\r\n")
@@ -94,7 +99,15 @@ fn check_toml(
                 format!("Dependencies for {} are not sorted", krate.to_string_lossy()),
             )?;
         }
-        return Ok(is_sorted);
+
+        if !is_formatted {
+            write_red(
+                "error: ",
+                format!("Cargo.toml for {} is not formatted", krate.to_string_lossy()),
+            )?;
+        }
+
+        return Ok(is_sorted && is_formatted);
     }
 
     write_file(&path, &sorted_str)?;
@@ -120,7 +133,7 @@ fn _main() -> IoResult<()> {
                     .help("sets cwd, must contain a Cargo.toml file"),
             )
             .arg(Arg::with_name("check").short("c").long("check").help(
-                "non-zero exit if Cargo.toml is unsorted, overrides default behavior",
+                "Returns non-zero exit code if Cargo.toml is unsorted, overrides default behavior",
             ))
             .arg(
                 Arg::with_name("print")
@@ -128,27 +141,31 @@ fn _main() -> IoResult<()> {
                     .long("print")
                     // No printing if we are running a --check
                     .conflicts_with("check")
-                    .help("prints Cargo.toml, lexically sorted, to stdout"),
+                    .help("Prints Cargo.toml, lexically sorted, to stdout"),
             )
             .arg(
                 Arg::with_name("no-format")
                     .short("n")
                     .long("no-format")
-                    // Force this arg to be present if --check is
-                    .default_value_if("check", None, "")
-                    .help("skip formatting after sorting"),
+                    .help("Skips formatting after sorting"),
+            )
+            .arg(
+                Arg::with_name("check-format")
+                    .requires("check")
+                    .long("check-format")
+                    .help("Also returns non-zero exit code if formatting changes"),
             )
             .arg(
                 Arg::with_name("workspace")
                     .short("w")
                     .long("workspace")
-                    .help("checks every crate in a workspace"),
+                    .help("Checks every crate in a workspace"),
             )
             .arg(
                 Arg::with_name("grouped")
                     .short("g")
                     .long("grouped")
-                    .help("when sorting groups of key value pairs blank lines are kept"),
+                    .help("Keep blank lines when sorting groups of key value pairs"),
             )
             .arg(
                 Arg::with_name("order")
@@ -157,7 +174,7 @@ fn _main() -> IoResult<()> {
                     .takes_value(true)
                     .empty_values(false)
                     .value_delimiter(",")
-                    .help("when sorting groups of key value pairs blank lines are kept"),
+                    .help("Keep blank lines when sorting groups of key value pairs"),
             )
             .after_help(EXTRA_HELP)
             .get_matches();


Test patch: