Repository: astral-sh/ruff
File: crates/ty_test/src/lib.rs (Rust)

Code before:

                for (relative_line_number, failures) in test_failures.by_line.iter() {
                    let (absolute_line_number, line_number_error) =
                        match source_map.to_absolute_line_number(relative_line_number) {
                            Ok(line_number) => (Some(line_number), None),
                            Err(err) => (
                                source_map.last_absolute_line_number(),
                                Some(err.to_string()),
                            ),
                        };

                    let absolute_line_number_value = absolute_line_number.map(OneIndexed::get);
                    let cli_line_info = match absolute_line_number_value {
                        Some(line) => format!("{relative_fixture_path}:{line}"),
                        None => format!("{relative_fixture_path}:?"),
                    };

                    match output_format {
                        OutputFormat::Cli => {
                            for failure in failures {
                                println!("  {} {failure}", cli_line_info.as_str().cyan());
                            }
                            if let Some(err) = line_number_error.as_deref() {
                                println!("  {} {err}", cli_line_info.as_str().cyan());
                            }
                        }
                        OutputFormat::GitHub => {
                            for failure in failures {
                                if let Some(line) = absolute_line_number_value {
                                    println!(
                                        "::error file={absolute_fixture_path},line={line}::{failure}"
                                    );
                                } else {
                                    println!("::error file={absolute_fixture_path}::{failure}");
                                }
                            }
                            if let Some(err) = line_number_error.as_deref() {
                                if let Some(line) = absolute_line_number_value {
                                    println!(
                                        "::error file={absolute_fixture_path},line={line}::{err}"
                                    );
                                } else {
                                    println!("::error file={absolute_fixture_path}::{err}");
                                }
                            }
                        }
                    }
                }
            }
        }
        if let Err(inconsistencies) = inconsistencies {

Review comment (bug):
Nit: to reduce code duplication, I suggest adding a method to `OutputFormat` to print an error


```rust
impl OutputFormat {
	fn print_error(self, file: &str, line: Option<OneIndexed>, failure: impl Display) {
		match self {
			Self::Cli => {
				let cli_line_info = match absolute_line_number_value {
		        Some(line) => format!("{relative_fixture_path}:{line}"),
		        None => format!("{relative_fixture_path}:?"),
		    };
				println!(" {} {failure}", cli_line_info.as_str().cyan());
			}
			Self::GitHub => {
				if let Some(line) = absolute_line_number_value {
					println!("::error file={absolute_fixture_path},line={line}::{err}");
				} else {
					println!("::error file={absolute_fixture_path}::{err}");
				}
			}
		}
	}
}

Code after:

                for (relative_line_number, failures) in test_failures.by_line.iter() {
                    let file = match output_format {
                        OutputFormat::Cli => relative_fixture_path.as_str(),
                        OutputFormat::GitHub => absolute_fixture_path.as_str(),
                    };

                    let absolute_line_number = match source_map
                        .to_absolute_line_number(relative_line_number)
                    {
                        Ok(line_number) => line_number,
                        Err(last_line_number) => {
                            print!("{}",
                                output_format.display_error(
                                    file,
                                    last_line_number,
                                    "Found a trailing assertion comment (e.g., `# revealed:` or `# error:`) \
                                    not followed by any statement."
                                )
                            );

                            continue;
                        }
                    };

                    for failure in failures {
                        print!(
                            "{}",
                            output_format.display_error(file, absolute_line_number, failure)
                        );
                    }
                }
            }
        }
        if let Err(inconsistencies) = inconsistencies {
            any_failures = true;
            for inconsistency in inconsistencies {
                match output_format {
                    OutputFormat::Cli => {
                        let info = relative_fixture_path.to_string().cyan();
                        println!("  {info} {inconsistency}");
                    }
                    OutputFormat::GitHub => {
                        println!("::error file={absolute_fixture_path}::{inconsistency}");
                    }
                }
            }
        }

        if this_test_failed && output_format.is_cli() {
            let escaped_test_name = test.name().replace('\'', "\\'");