This is a migrated thread and some comments may be shown as answers.

Two analysis issues

3 Answers 75 Views
Code Analysis
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jerremy
Top achievements
Rank 1
Jerremy asked on 01 Dec 2009, 03:19 PM
First one is Linq related, the following code snippet will give a "C#: This cast is not required", but it is ;)

var result = from somerecord in context.sometable 
select new { Test = (SomeEnum?)somerecord.ChildTable.FirstOrDefault().Test  }; 

JustCode will note that the typecast (SomeEnum?) is not needed, however removing that type cast will give a "The null value cannot be assigned to a member with type SomeEnum which is a non-nullable type.".  (note the above snippet is pseudo code as my actual code wouldnt be much use for you either, but to make it work the childtable should have the field Test and it has to be of the type SomeEnum and the record in sometable has to exist but the record in childtable shouldnt)

One small note, it only seemed to apply to the enumerator, if I type-casted an int to int? it didnt give me the warning.

The other issue is related to this code snippet:
<%@ Page Language="C#" ContentType="text/javascript" %> 
<%@ Import Namespace="Moxiecode.Manager.Utils"%> 
 
<
    string[] classes = Request["classes"].Split(new[] { ',' }); 
 
    var compressor = new JSCompressor(); 
 
    foreach (string file in classes) 
        compressor.AddFile(file.ToLower().Replace('.', '/') + ".js"); 
 
    compressor.Compress(Request, Response); 
%> 
 

JustCode freaks out on this, my guess is that it incorrectly parses the ContentType (which only indicates the output of the page) thinking that the rest of the page is JavaScript, however the language clearly indicates its C#.

Also a minor issue, one of our old projects still uses 'good' old xsd file (in the app_code folder of a website) for the DAL, JustCode pretty much fails to detect anything defined in there. I'm not even sure if you can still create xsd in VS2008 but its a legacy project.

3 Answers, 1 is accepted

Sort by
0
Jordan
Telerik team
answered on 03 Dec 2009, 08:56 AM
Hello Jerremy,

Thanks for the feedback.
1. Linq related issue
I try to reproduce it with some custom classes but I couldn't. I attached a sample project. Can you modify it and send it back to us?

2. C# in file with ContentType="text/javascript"
Yes, we parse the file according to the ContentType, but in this case we have a bug. I've added it to our todo list.

3. DataSets
We're aware of the problem about using DataSets and DataAdapters in web site projects. Hopefully we'll manage to fix it soon.

Regards,
Jordan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Jerremy
Top achievements
Rank 1
answered on 03 Dec 2009, 10:22 AM

Hi,

It seems its only an "issue" with Linq2SQL. In your project (Linq2Classes) it actually fails no matter what, probably due to L2S converting the Linq query into SQL (it will handle the null from the FirstOrDefault differently).

I have a test project which shows the issue but I cant attach it (I seem to only be able to attach images).

So the short story is this:

var context = new DatabaseDataContext();  
              
// Jerremy: This will work because its being type casted to SomeEnum?  
var result = from somerecord in context.SomeTables  
             select new Test2{ Test = (SomeEnum?)somerecord.ChildTables.FirstOrDefault().Test};  
              
// Jerremy: This will work, again because of the typecast to SomeEnum?  
//          also note that the typecast to (int?) doesnt give the warning  
var result2 = from somerecord in context.SomeTables  
              select new {  
                         Test = (SomeEnum?)somerecord.ChildTables.FirstOrDefault().Test,  
                         Test2 = (int?)somerecord.ChildTables.FirstOrDefault().Test2  
                         };  
 
// Jerremy: This wont work due to the missing typecast  
var result3 = from somerecord in context.SomeTables  
              select new { Test = somerecord.ChildTables.FirstOrDefault().Test };  
 
// Works  
Console.WriteLine(result.First().Test);  
 
// Works
Console.WriteLine(result2.First().Test);  
 
// Will fail  
Console.WriteLine(result3.First().Test); 

I created a dbml from the database (see sql script). I made the "Test" field a "SomeEnum" and I left the "Test2" field an integer.

Tested against this database:
USE [master]  
GO  
 
/****** Object:  Database [JustCode]    Script Date: 12/03/2009 10:41:02 ******/  
CREATE DATABASE [JustCode] ON  PRIMARY   
NAME = N'JustCode', FILENAME = N'D:\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\JustCode.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )  
 LOG ON   
NAME = N'JustCode_log', FILENAME = N'D:\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\JustCode_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)  
GO  
 
ALTER DATABASE [JustCode] SET COMPATIBILITY_LEVEL = 100  
GO  
 
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))  
begin 
EXEC [JustCode].[dbo].[sp_fulltext_database] @action = 'enable' 
end 
GO  
 
ALTER DATABASE [JustCode] SET ANSI_NULL_DEFAULT OFF   
GO  
 
ALTER DATABASE [JustCode] SET ANSI_NULLS OFF   
GO  
 
ALTER DATABASE [JustCode] SET ANSI_PADDING OFF   
GO  
 
ALTER DATABASE [JustCode] SET ANSI_WARNINGS OFF   
GO  
 
ALTER DATABASE [JustCode] SET ARITHABORT OFF   
GO  
 
ALTER DATABASE [JustCode] SET AUTO_CLOSE ON   
GO  
 
ALTER DATABASE [JustCode] SET AUTO_CREATE_STATISTICS ON   
GO  
 
ALTER DATABASE [JustCode] SET AUTO_SHRINK OFF   
GO  
 
ALTER DATABASE [JustCode] SET AUTO_UPDATE_STATISTICS ON   
GO  
 
ALTER DATABASE [JustCode] SET CURSOR_CLOSE_ON_COMMIT OFF   
GO  
 
ALTER DATABASE [JustCode] SET CURSOR_DEFAULT  GLOBAL   
GO  
 
ALTER DATABASE [JustCode] SET CONCAT_NULL_YIELDS_NULL OFF   
GO  
 
ALTER DATABASE [JustCode] SET NUMERIC_ROUNDABORT OFF   
GO  
 
ALTER DATABASE [JustCode] SET QUOTED_IDENTIFIER OFF   
GO  
 
ALTER DATABASE [JustCode] SET RECURSIVE_TRIGGERS OFF   
GO  
 
ALTER DATABASE [JustCode] SET  DISABLE_BROKER   
GO  
 
ALTER DATABASE [JustCode] SET AUTO_UPDATE_STATISTICS_ASYNC OFF   
GO  
 
ALTER DATABASE [JustCode] SET DATE_CORRELATION_OPTIMIZATION OFF   
GO  
 
ALTER DATABASE [JustCode] SET TRUSTWORTHY OFF   
GO  
 
ALTER DATABASE [JustCode] SET ALLOW_SNAPSHOT_ISOLATION OFF   
GO  
 
ALTER DATABASE [JustCode] SET PARAMETERIZATION SIMPLE   
GO  
 
ALTER DATABASE [JustCode] SET READ_COMMITTED_SNAPSHOT OFF   
GO  
 
ALTER DATABASE [JustCode] SET HONOR_BROKER_PRIORITY OFF   
GO  
 
ALTER DATABASE [JustCode] SET  READ_WRITE   
GO  
 
ALTER DATABASE [JustCode] SET RECOVERY SIMPLE   
GO  
 
ALTER DATABASE [JustCode] SET  MULTI_USER   
GO  
 
ALTER DATABASE [JustCode] SET PAGE_VERIFY CHECKSUM    
GO  
 
ALTER DATABASE [JustCode] SET DB_CHAINING OFF   
GO  
 
 
USE [JustCode]  
GO  
 
/****** Object:  Table [dbo].[SomeTable]    Script Date: 12/03/2009 10:40:21 ******/  
SET ANSI_NULLS ON 
GO  
 
SET QUOTED_IDENTIFIER ON 
GO  
 
CREATE TABLE [dbo].[SomeTable](  
    [Id] [intNOT NULL,  
 CONSTRAINT [PK_SomeTable] PRIMARY KEY CLUSTERED   
(  
    [Id] ASC 
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ONON [PRIMARY]  
ON [PRIMARY]  
 
GO  
 
 
USE [JustCode]  
GO  
 
/****** Object:  Table [dbo].[ChildTable]    Script Date: 12/03/2009 10:40:50 ******/  
SET ANSI_NULLS ON 
GO  
 
SET QUOTED_IDENTIFIER ON 
GO  
 
CREATE TABLE [dbo].[ChildTable](  
    [Id] [intNOT NULL,  
    [SomeTableId] [intNOT NULL,  
    [Test] [intNOT NULL,  
    [Test2] [intNOT NULL,  
 CONSTRAINT [PK_ChildTable] PRIMARY KEY CLUSTERED   
(  
    [Id] ASC 
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ONON [PRIMARY]  
ON [PRIMARY]  
 
GO  
 
ALTER TABLE [dbo].[ChildTable]  WITH CHECK ADD  CONSTRAINT [FK_ChildTable_SomeTable] FOREIGN KEY([SomeTableId])  
REFERENCES [dbo].[SomeTable] ([Id])  
GO  
 
ALTER TABLE [dbo].[ChildTable] CHECK CONSTRAINT [FK_ChildTable_SomeTable]  
GO  
 
INSERT INTO [dbo].[SomeTable] (Id) values (1)  
GO 

Small note with the script, you will probably have to change the DB-create part.
0
Jordan
Telerik team
answered on 03 Dec 2009, 04:23 PM
Hello Jerremy,

Thanks a lot for taking some time to help us reproduce the problem! We will make sure not to show incorrect warnings if the casts affect the inferred types. I added the issue to our todo list.


All the best,
Jordan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Code Analysis
Asked by
Jerremy
Top achievements
Rank 1
Answers by
Jordan
Telerik team
Jerremy
Top achievements
Rank 1
Share this question
or