11//! The output of a compiled project
22use contracts:: { VersionedContract , VersionedContracts } ;
33use foundry_compilers_artifacts:: {
4- CompactContractBytecode , CompactContractRef , Contract , ErrorFilter , Severity ,
4+ CompactContractBytecode , CompactContractRef , Contract , Severity ,
55} ;
66use foundry_compilers_core:: error:: { SolcError , SolcIoError } ;
77use info:: ContractInfoRef ;
@@ -483,8 +483,7 @@ impl<C: Compiler, T: ArtifactOutput> ProjectCompileOutput<C, T> {
483483
484484 /// Returns whether any warnings were emitted by the compiler.
485485 pub fn has_compiler_warnings ( & self ) -> bool {
486- let filter = ErrorFilter :: new ( & self . ignored_error_codes , & self . ignored_file_paths ) ;
487- self . compiler_output . has_warning ( filter)
486+ self . compiler_output . has_warning ( & self . ignored_error_codes , & self . ignored_file_paths )
488487 }
489488
490489 /// Panics if any errors were emitted by the compiler.
@@ -836,8 +835,7 @@ impl<C: Compiler> AggregatedCompilerOutput<C> {
836835 if compiler_severity_filter. ge ( & err. severity ( ) ) {
837836 if compiler_severity_filter. is_warning ( ) {
838837 // skip ignored error codes and file path from warnings
839- let filter = ErrorFilter :: new ( ignored_error_codes, ignored_file_paths) ;
840- return self . has_warning ( filter) ;
838+ return self . has_warning ( ignored_error_codes, ignored_file_paths) ;
841839 }
842840 return true ;
843841 }
@@ -847,25 +845,52 @@ impl<C: Compiler> AggregatedCompilerOutput<C> {
847845
848846 /// Checks if there are any compiler warnings that are not ignored by the specified error codes
849847 /// and file paths.
850- pub fn has_warning < ' a > ( & self , filter : impl Into < ErrorFilter < ' a > > ) -> bool {
851- let filter: ErrorFilter < ' _ > = filter. into ( ) ;
852- self . errors . iter ( ) . any ( |error| {
853- if !error. is_warning ( ) {
854- return false ;
848+ pub fn has_warning ( & self , ignored_error_codes : & [ u64 ] , ignored_file_paths : & [ PathBuf ] ) -> bool {
849+ self . errors
850+ . iter ( )
851+ . any ( |error| !self . should_ignore ( ignored_error_codes, ignored_file_paths, error) )
852+ }
853+
854+ pub fn should_ignore (
855+ & self ,
856+ ignored_error_codes : & [ u64 ] ,
857+ ignored_file_paths : & [ PathBuf ] ,
858+ error : & C :: CompilationError ,
859+ ) -> bool {
860+ if !error. is_warning ( ) {
861+ return false ;
862+ }
863+
864+ let mut ignore = false ;
865+
866+ if let Some ( code) = error. error_code ( ) {
867+ ignore |= ignored_error_codes. contains ( & code) ;
868+ if let Some ( loc) = error. source_location ( ) {
869+ let path = Path :: new ( & loc. file ) ;
870+ ignore |=
871+ ignored_file_paths. iter ( ) . any ( |ignored_path| path. starts_with ( ignored_path) ) ;
872+
873+ // we ignore spdx and contract size warnings in test
874+ // files. if we are looking at one of these warnings
875+ // from a test file we skip
876+ ignore |= self . is_test ( path) && ( code == 1878 || code == 5574 ) ;
855877 }
878+ }
856879
857- let is_code_ignored = filter. is_code_ignored ( error. error_code ( ) ) ;
880+ ignore
881+ }
858882
859- let is_file_ignored = error
860- . source_location ( )
861- . as_ref ( )
862- . map_or ( false , |location| filter. is_file_ignored ( Path :: new ( & location. file ) ) ) ;
883+ /// Returns true if the contract is a expected to be a test
884+ fn is_test ( & self , contract_path : & Path ) -> bool {
885+ if contract_path. to_string_lossy ( ) . ends_with ( ".t.sol" ) {
886+ return true ;
887+ }
863888
864- // Only consider warnings that are not ignored by either code or file path.
865- // Hence, return `true` for warnings that are not ignored, making the function
866- // return `true` if any such warnings exist.
867- ! ( is_code_ignored || is_file_ignored )
868- } )
889+ self . contracts . contracts_with_files ( ) . filter ( | ( path , _ , _ ) | * path == contract_path ) . any (
890+ | ( _ , _ , contract ) | {
891+ contract . abi . as_ref ( ) . map_or ( false , |abi| abi . functions . contains_key ( "IS_TEST" ) )
892+ } ,
893+ )
869894 }
870895}
871896
@@ -894,19 +919,7 @@ impl<'a, C: Compiler> OutputDiagnostics<'a, C> {
894919
895920 /// Returns true if there is at least one warning
896921 pub fn has_warning ( & self ) -> bool {
897- let filter = ErrorFilter :: new ( self . ignored_error_codes , self . ignored_file_paths ) ;
898- self . compiler_output . has_warning ( filter)
899- }
900-
901- /// Returns true if the contract is a expected to be a test
902- fn is_test ( & self , contract_path : & str ) -> bool {
903- if contract_path. ends_with ( ".t.sol" ) {
904- return true ;
905- }
906-
907- self . compiler_output . find_first ( contract_path) . map_or ( false , |contract| {
908- contract. abi . map_or ( false , |abi| abi. functions . contains_key ( "IS_TEST" ) )
909- } )
922+ self . compiler_output . has_warning ( self . ignored_error_codes , self . ignored_file_paths )
910923 }
911924}
912925
@@ -923,29 +936,11 @@ impl<'a, C: Compiler> fmt::Display for OutputDiagnostics<'a, C> {
923936 . fmt ( f) ?;
924937
925938 for err in & self . compiler_output . errors {
926- let mut ignored = false ;
927- if err. is_warning ( ) {
928- if let Some ( code) = err. error_code ( ) {
929- if let Some ( source_location) = & err. source_location ( ) {
930- // we ignore spdx and contract size warnings in test
931- // files. if we are looking at one of these warnings
932- // from a test file we skip
933- ignored =
934- self . is_test ( & source_location. file ) && ( code == 1878 || code == 5574 ) ;
935-
936- // we ignore warnings coming from ignored files
937- let source_path = Path :: new ( & source_location. file ) ;
938- ignored |= self
939- . ignored_file_paths
940- . iter ( )
941- . any ( |ignored_path| source_path. starts_with ( ignored_path) ) ;
942- }
943-
944- ignored |= self . ignored_error_codes . contains ( & code) ;
945- }
946- }
947-
948- if !ignored {
939+ if !self . compiler_output . should_ignore (
940+ self . ignored_error_codes ,
941+ self . ignored_file_paths ,
942+ err,
943+ ) {
949944 f. write_str ( "\n " ) ?;
950945 fmt:: Display :: fmt ( & err, f) ?;
951946 }
0 commit comments