@@ -39,7 +39,7 @@ pub fn generate_noqa_edits(
3939 let exemption = FileExemption :: from ( & file_directives) ;
4040 let directives = NoqaDirectives :: from_commented_ranges ( comment_ranges, external, path, locator) ;
4141 let comments = find_noqa_comments ( diagnostics, locator, & exemption, & directives, noqa_line_for) ;
42- build_noqa_edits_by_diagnostic ( comments, locator, line_ending)
42+ build_noqa_edits_by_diagnostic ( comments, locator, line_ending, None )
4343}
4444
4545/// A directive to ignore a set of rules either for a given line of Python source code or an entire file (e.g.,
@@ -715,6 +715,7 @@ impl Display for LexicalError {
715715impl Error for LexicalError { }
716716
717717/// Adds noqa comments to suppress all messages of a file.
718+ #[ expect( clippy:: too_many_arguments) ]
718719pub ( crate ) fn add_noqa (
719720 path : & Path ,
720721 diagnostics : & [ Diagnostic ] ,
@@ -723,6 +724,7 @@ pub(crate) fn add_noqa(
723724 external : & [ String ] ,
724725 noqa_line_for : & NoqaMapping ,
725726 line_ending : LineEnding ,
727+ reason : Option < & str > ,
726728) -> Result < usize > {
727729 let ( count, output) = add_noqa_inner (
728730 path,
@@ -732,12 +734,14 @@ pub(crate) fn add_noqa(
732734 external,
733735 noqa_line_for,
734736 line_ending,
737+ reason,
735738 ) ;
736739
737740 fs:: write ( path, output) ?;
738741 Ok ( count)
739742}
740743
744+ #[ expect( clippy:: too_many_arguments) ]
741745fn add_noqa_inner (
742746 path : & Path ,
743747 diagnostics : & [ Diagnostic ] ,
@@ -746,6 +750,7 @@ fn add_noqa_inner(
746750 external : & [ String ] ,
747751 noqa_line_for : & NoqaMapping ,
748752 line_ending : LineEnding ,
753+ reason : Option < & str > ,
749754) -> ( usize , String ) {
750755 let mut count = 0 ;
751756
@@ -757,7 +762,7 @@ fn add_noqa_inner(
757762
758763 let comments = find_noqa_comments ( diagnostics, locator, & exemption, & directives, noqa_line_for) ;
759764
760- let edits = build_noqa_edits_by_line ( comments, locator, line_ending) ;
765+ let edits = build_noqa_edits_by_line ( comments, locator, line_ending, reason ) ;
761766
762767 let contents = locator. contents ( ) ;
763768
@@ -783,6 +788,7 @@ fn build_noqa_edits_by_diagnostic(
783788 comments : Vec < Option < NoqaComment > > ,
784789 locator : & Locator ,
785790 line_ending : LineEnding ,
791+ reason : Option < & str > ,
786792) -> Vec < Option < Edit > > {
787793 let mut edits = Vec :: default ( ) ;
788794 for comment in comments {
@@ -794,6 +800,7 @@ fn build_noqa_edits_by_diagnostic(
794800 FxHashSet :: from_iter ( [ comment. code ] ) ,
795801 locator,
796802 line_ending,
803+ reason,
797804 ) {
798805 edits. push ( Some ( noqa_edit. into_edit ( ) ) ) ;
799806 }
@@ -808,6 +815,7 @@ fn build_noqa_edits_by_line<'a>(
808815 comments : Vec < Option < NoqaComment < ' a > > > ,
809816 locator : & Locator ,
810817 line_ending : LineEnding ,
818+ reason : Option < & ' a str > ,
811819) -> BTreeMap < TextSize , NoqaEdit < ' a > > {
812820 let mut comments_by_line = BTreeMap :: default ( ) ;
813821 for comment in comments. into_iter ( ) . flatten ( ) {
@@ -831,6 +839,7 @@ fn build_noqa_edits_by_line<'a>(
831839 . collect ( ) ,
832840 locator,
833841 line_ending,
842+ reason,
834843 ) {
835844 edits. insert ( offset, edit) ;
836845 }
@@ -927,6 +936,7 @@ struct NoqaEdit<'a> {
927936 noqa_codes : FxHashSet < & ' a SecondaryCode > ,
928937 codes : Option < & ' a Codes < ' a > > ,
929938 line_ending : LineEnding ,
939+ reason : Option < & ' a str > ,
930940}
931941
932942impl NoqaEdit < ' _ > {
@@ -954,6 +964,9 @@ impl NoqaEdit<'_> {
954964 push_codes ( writer, self . noqa_codes . iter ( ) . sorted_unstable ( ) ) ;
955965 }
956966 }
967+ if let Some ( reason) = self . reason {
968+ write ! ( writer, " {reason}" ) . unwrap ( ) ;
969+ }
957970 write ! ( writer, "{}" , self . line_ending. as_str( ) ) . unwrap ( ) ;
958971 }
959972}
@@ -970,6 +983,7 @@ fn generate_noqa_edit<'a>(
970983 noqa_codes : FxHashSet < & ' a SecondaryCode > ,
971984 locator : & Locator ,
972985 line_ending : LineEnding ,
986+ reason : Option < & ' a str > ,
973987) -> Option < NoqaEdit < ' a > > {
974988 let line_range = locator. full_line_range ( offset) ;
975989
@@ -999,6 +1013,7 @@ fn generate_noqa_edit<'a>(
9991013 noqa_codes,
10001014 codes,
10011015 line_ending,
1016+ reason,
10021017 } )
10031018}
10041019
@@ -2832,6 +2847,7 @@ mod tests {
28322847 & [ ] ,
28332848 & noqa_line_for,
28342849 LineEnding :: Lf ,
2850+ None ,
28352851 ) ;
28362852 assert_eq ! ( count, 0 ) ;
28372853 assert_eq ! ( output, format!( "{contents}" ) ) ;
@@ -2855,6 +2871,7 @@ mod tests {
28552871 & [ ] ,
28562872 & noqa_line_for,
28572873 LineEnding :: Lf ,
2874+ None ,
28582875 ) ;
28592876 assert_eq ! ( count, 1 ) ;
28602877 assert_eq ! ( output, "x = 1 # noqa: F841\n " ) ;
@@ -2885,6 +2902,7 @@ mod tests {
28852902 & [ ] ,
28862903 & noqa_line_for,
28872904 LineEnding :: Lf ,
2905+ None ,
28882906 ) ;
28892907 assert_eq ! ( count, 1 ) ;
28902908 assert_eq ! ( output, "x = 1 # noqa: E741, F841\n " ) ;
@@ -2915,6 +2933,7 @@ mod tests {
29152933 & [ ] ,
29162934 & noqa_line_for,
29172935 LineEnding :: Lf ,
2936+ None ,
29182937 ) ;
29192938 assert_eq ! ( count, 0 ) ;
29202939 assert_eq ! ( output, "x = 1 # noqa" ) ;
0 commit comments