@@ -20,7 +20,7 @@ use ruff_linter::settings::{LinterSettings, flags};
2020use ruff_linter:: source_kind:: { SourceError , SourceKind } ;
2121use ruff_linter:: { IOError , Violation , fs} ;
2222use ruff_notebook:: { NotebookError , NotebookIndex } ;
23- use ruff_python_ast:: { PySourceType , SourceType , TomlSourceType } ;
23+ use ruff_python_ast:: { SourceType , TomlSourceType } ;
2424use ruff_source_file:: SourceFileBuilder ;
2525use ruff_text_size:: TextRange ;
2626use ruff_workspace:: Settings ;
@@ -208,35 +208,31 @@ pub(crate) fn lint_path(
208208
209209 debug ! ( "Checking: {}" , path. display( ) ) ;
210210
211- let source_type = match settings. extension . get ( path) . map ( PySourceType :: from) {
212- Some ( source_type) => source_type,
213- None => match SourceType :: from ( path) {
214- SourceType :: Toml ( TomlSourceType :: Pyproject ) => {
215- let diagnostics = if settings
216- . rules
217- . iter_enabled ( )
218- . any ( |rule_code| rule_code. lint_source ( ) . is_pyproject_toml ( ) )
219- {
220- let contents = match std:: fs:: read_to_string ( path) . map_err ( SourceError :: from) {
221- Ok ( contents) => contents,
222- Err ( err) => {
223- return Ok ( Diagnostics :: from_source_error ( & err, Some ( path) , settings) ) ;
224- }
225- } ;
226- let source_file =
227- SourceFileBuilder :: new ( path. to_string_lossy ( ) , contents) . finish ( ) ;
228- lint_pyproject_toml ( & source_file, settings)
229- } else {
230- vec ! [ ]
211+ let source_type = match settings. extension . get_source_type ( path) {
212+ SourceType :: Toml ( TomlSourceType :: Pyproject ) => {
213+ let diagnostics = if settings
214+ . rules
215+ . iter_enabled ( )
216+ . any ( |rule_code| rule_code. lint_source ( ) . is_pyproject_toml ( ) )
217+ {
218+ let contents = match std:: fs:: read_to_string ( path) . map_err ( SourceError :: from) {
219+ Ok ( contents) => contents,
220+ Err ( err) => {
221+ return Ok ( Diagnostics :: from_source_error ( & err, Some ( path) , settings) ) ;
222+ }
231223 } ;
232- return Ok ( Diagnostics {
233- inner : diagnostics,
234- ..Diagnostics :: default ( )
235- } ) ;
236- }
237- SourceType :: Toml ( _) | SourceType :: Markdown => return Ok ( Diagnostics :: default ( ) ) ,
238- SourceType :: Python ( source_type) => source_type,
239- } ,
224+ let source_file = SourceFileBuilder :: new ( path. to_string_lossy ( ) , contents) . finish ( ) ;
225+ lint_pyproject_toml ( & source_file, settings)
226+ } else {
227+ vec ! [ ]
228+ } ;
229+ return Ok ( Diagnostics {
230+ inner : diagnostics,
231+ ..Diagnostics :: default ( )
232+ } ) ;
233+ }
234+ SourceType :: Toml ( _) | SourceType :: Markdown => return Ok ( Diagnostics :: default ( ) ) ,
235+ SourceType :: Python ( source_type) => source_type,
240236 } ;
241237
242238 // Extract the sources from the file.
@@ -356,42 +352,37 @@ pub(crate) fn lint_stdin(
356352 fix_mode : flags:: FixMode ,
357353) -> Result < Diagnostics > {
358354 let ( source_type, py_source_type) = match path
359- . and_then ( |path| settings. linter . extension . get ( path) )
355+ . map ( |path| settings. linter . extension . get_source_type ( path) )
356+ . unwrap_or_default ( )
360357 {
361- None => match path. map ( SourceType :: from) . unwrap_or_default ( ) {
362- SourceType :: Toml ( source_type) if source_type. is_pyproject ( ) => {
363- if !settings
364- . linter
365- . rules
366- . iter_enabled ( )
367- . any ( |rule_code| rule_code. lint_source ( ) . is_pyproject_toml ( ) )
368- {
369- return Ok ( Diagnostics :: default ( ) ) ;
370- }
358+ SourceType :: Toml ( source_type) if source_type. is_pyproject ( ) => {
359+ if !settings
360+ . linter
361+ . rules
362+ . iter_enabled ( )
363+ . any ( |rule_code| rule_code. lint_source ( ) . is_pyproject_toml ( ) )
364+ {
365+ return Ok ( Diagnostics :: default ( ) ) ;
366+ }
371367
372- let path = path. unwrap ( ) ;
373- let source_file =
374- SourceFileBuilder :: new ( path. to_string_lossy ( ) , contents. clone ( ) ) . finish ( ) ;
368+ let path = path. unwrap ( ) ;
369+ let source_file =
370+ SourceFileBuilder :: new ( path. to_string_lossy ( ) , contents. clone ( ) ) . finish ( ) ;
375371
376- match fix_mode {
377- flags:: FixMode :: Diff | flags:: FixMode :: Generate => { }
378- flags:: FixMode :: Apply => write ! ( & mut io:: stdout( ) . lock( ) , "{contents}" ) ?,
379- }
380-
381- return Ok ( Diagnostics {
382- inner : lint_pyproject_toml ( & source_file, & settings. linter ) ,
383- fixed : FixMap :: from_iter ( [ ( fs:: relativize_path ( path) , FixTable :: default ( ) ) ] ) ,
384- notebook_indexes : FxHashMap :: default ( ) ,
385- } ) ;
372+ match fix_mode {
373+ flags:: FixMode :: Diff | flags:: FixMode :: Generate => { }
374+ flags:: FixMode :: Apply => write ! ( & mut io:: stdout( ) . lock( ) , "{contents}" ) ?,
386375 }
387376
388- SourceType :: Toml ( _) | SourceType :: Markdown => return Ok ( Diagnostics :: default ( ) ) ,
389- source_type @ SourceType :: Python ( py_source_type) => ( source_type, py_source_type) ,
390- } ,
391- Some ( language) => {
392- let py_source_type = PySourceType :: from ( language) ;
393- ( SourceType :: Python ( py_source_type) , py_source_type)
377+ return Ok ( Diagnostics {
378+ inner : lint_pyproject_toml ( & source_file, & settings. linter ) ,
379+ fixed : FixMap :: from_iter ( [ ( fs:: relativize_path ( path) , FixTable :: default ( ) ) ] ) ,
380+ notebook_indexes : FxHashMap :: default ( ) ,
381+ } ) ;
394382 }
383+
384+ SourceType :: Toml ( _) | SourceType :: Markdown => return Ok ( Diagnostics :: default ( ) ) ,
385+ source_type @ SourceType :: Python ( py_source_type) => ( source_type, py_source_type) ,
395386 } ;
396387
397388 // Extract the sources from the file.
0 commit comments